当从构造函数抛出异常时,应用程序崩溃
Application crashes when exception is thrown from constructor
当从类构造函数抛出异常时,程序崩溃。在调试模式下运行时,我得到以下错误"VirtualCtor.exe中0x74A2DB18的未处理异常:Microsoft c++异常:[rethrow]在内存位置0x00000000。"它在发布模式下也会崩溃。我理解这是因为throw没有找到确切的catch处理程序,并且调用了std::terminate()。但是,我的理解是,catch(…)应该处理这个问题。有人能让我知道确切的处理程序,我需要与捕获使用?
#include<iostream>
#include<exception>
using namespace std;
class MyClass
{
public:
MyClass() { cout << "Default Ctor" << endl;
throw; //runtime exception.
}
~MyClass()
{
cout << "Destructor called" << endl;
}
};
int main()
{
MyClass*vpt = nullptr;
try {
vpt = new MyClass;
}
catch (...) {
delete vpt;
cout << "Exception"<< endl;
}
return 0;
}
修改代码throw bad_alloc();捕获异常和代码崩溃不再,但我需要了解发生了什么只是从函数/构造函数调用抛出?
谢谢。
你没有抛出异常。您只是在编写throw
,它重新抛出已经抛出的异常。在本例中,没有定义,因此程序具有未定义行为。所以才会出现崩盘。
如果你想扔东西,你就得扔东西!
MyClass()
{
cout << "Default Ctor" << endl;
throw std::runtime_exception("Testing exception handling");
}
相关文章:
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 如何找出应用程序崩溃的原因 - Win 10 LTSB
- 操纵安卓相机的深度图导致应用程序崩溃
- 调用 java 的回调() 时应用程序崩溃.由于 detatchThread 而获得运行时错误
- QCompleter set模型使应用程序崩溃
- Qt 5 应用程序崩溃并出现"qLineEdit::setText"
- 切换NvAPI_Stereo_Deactivate/NvAPI_Stereo_activate会使unity应用程序崩溃
- eglSwapBuffers上的应用程序崩溃
- VLD 使应用程序崩溃
- 由于调用 std::condition_variable 后参数无效而导致应用程序崩溃
- 执行递归函数时 C++ 应用程序崩溃
- 在 C++/CLI/C# 项目中启用"Native Code Debugging"导致应用程序崩溃
- LOCAL_SHARED_LIBRARIES使我的应用程序崩溃
- 为什么使用 nullPtr 调用函数不会使我的应用程序崩溃
- 当删除上下文属性中的QLIST对象时,QT QML应用程序崩溃
- React 本机 0.59 应用程序崩溃并出现 libc 错误
- 当使用“制造”编译的程序中使用库时,应用程序崩溃
- 如何在导致应用程序崩溃的代码中修复指令
- C++多线程应用程序崩溃
- 当使用 /ENTRY:main 和 /MT 运行时库编译为 /SUBSYSTEM:WINDOWS 时,应用程序崩溃