扩展std :: exception [英] Extend std::exception

查看:76
本文介绍了扩展std :: exception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是意大利人,我为我的英语道歉。


我想扩展类std :: exception:

Hello, i am italian, I apologize for my English.

I would like to extend the class std::exception:

展开 | 选择 | Wrap | 行号

推荐答案

如果std :: string抛出异常,则表示您没有捕获它。 std :: string将抛出std :: exception或更可能是从stdexcept头中的std :: exception派生的异常。


你抓住自己的异常,这不会是std :: string引发异常的匹配,它不能,std :: string在没有访问Exception类的情况下编译,因此不能通过该类。因此,不会输入异常处理程序,因为您必须捕获抛出的东西或抛出的东西的超类。然后,未处理的异常会导致程序进入核心转储。


您从std :: exception派生,以便您可以从您自己的代码中抛出异常,但是使用你的代码的人可以使用std :: exception以及所有其他异常轻松捕获。
If std::string is throwing an exception you are not catching it. std::string will be throwing either std::exception or more likely an exception derived from std::exception from the stdexcept header.

You catch your own exception, this will not be a match for the exception thrown by std::string, it can not be, std::string is compiled without access to your Exception class and so can not through that class. So the exception handler will not be entered because you have to catch the thing thrown or a super-class of the thing thrown. The unhandled exception then causes you program to core dump.


You derive from std::exception so that you have an exception that you can throw from your own code but that people using your code can catch easily using std::exception as well as all any other exceptions.


@dinopc,


我可以问为什么你是用std :: swap来实现赋值的,它会回复你的作业?这种递归很可能导致无限的堆栈扩展,最终导致核心转储。


这就是说,我真的不明白你修改的赋值运算符 - 你在尝试什么实现?


当然,我对你的代码的分析可能是错的,在这种情况下,请不理我。


干杯!
@dinopc,

May I ask why you''re implementing assignment in terms of std::swap, which recurses back to your assignment? This recursion may well cause an infinite stack extension, ultimately resulting in a core dump.

Which is to say, I really don''t understand your modified assignment operator - what are you trying to achieve?

Of course, my analysis of your code may be wrong, in which case, ignore me.

Cheers!


另外,当你从std :: exception派生时,你会捕获一个std :: exception引用而不是你自己的派生类的对象。


虚函数用于获取正确的what()调用。
Also, when you derive from std::exception, you catch an std::exception reference and not an object of your own derived class.

virtual functions are used to get the correct what() call.


这篇关于扩展std :: exception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆