预期的异常(C ++) [英] Exception expected (C++)

查看:76
本文介绍了预期的异常(C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习C ++,在我的教科书中有以下示例:

展开 | 选择 | Wrap | 行号

解决方案

如果抛出了bad_alloc异常,那么你需要捕获一个bad_alloc。


bad_alloc是派生的异常类,所以你也可以捕获异常。


请务必参考。也就是说,a)捕获bad_alloc&或b)捕获异常& ;.然后,您可以调用what()方法来查看字符串。


...或者您可以安装自己的''new_handler'',它应该自动咳出更多的内存。如果所有其他方法都失败了,你可以使用第二个参数''nothrow''调用new运算符,这会导致new不抛出异常。阅读
< br $>
亲切的问候,


Jos


好的,所以我添加了这个:

展开 | 选择 | Wrap | 行号< /跨度>

I''m learning C++ at the moment and in my textbook there is the following example:

Expand|Select|Wrap|Line Numbers

解决方案

If a bad_alloc exception is thrown, then you need to catch a bad_alloc.

bad_alloc is a derived class of exception so you could also catch an exception.

Be sure to do this by reference. That is, a) catch a bad_alloc&, or b) catch an exception&. You can then call the what() method to view the string.


... or you can install your own ''new_handler'' that is supposed to automagically cough up some more memory. If all else fails you can call the new operator with a second parameter ''nothrow'' that causes new not to throw an exception. Read this.

kind regards,

Jos


OK, so I added this:

Expand|Select|Wrap|Line Numbers


这篇关于预期的异常(C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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