函数try块。一个有趣的例子 [英] function try block. An interesting example
本文介绍了函数try块。一个有趣的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑下面的C ++程序
Consider the following C++ program
struct str
{
int mem;
str()
try
:mem(0)
{
throw 0;
}
catch(...)
{
}
};
int main()
{
str inst;
}
catch块工作,即控件到达它,然后程序崩溃。
The catch block works, i.e. the control reaches it, and then the program crashes. I can't understand what's wrong with it.
推荐答案
一旦控件到达函数try-catch的catch块的末尾,块的构造函数,异常被自动抛出。因为你不在main()中进一步捕获它,所以调用terminate()。
这是一个有趣的阅读: http://www.drdobbs.com/184401316
Once the control reaches the end of the catch block of function-try-block of a constructor, the exception is automatically rethrown. As you don't catch it further in main(), terminate() is called. Here is an interesting reading: http://www.drdobbs.com/184401316
这篇关于函数try块。一个有趣的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文