try{
//some function
}catch(...){
//write information to a file including this line number
}
-
是什么让我无法这样做,而不是为实例指定空指针异常?该程序无论如何都会崩溃,所以如果有什么我没想到的话,我也可能会捕获所有错误?
最佳答案
每个程序错误都会被catch(...)
捕获是一个神话。特别是,空指针取消引用的行为是不确定的,因此根本无法保证它会在那里被捕获。
如果您想要一个特别通用的catch
网站,那么捕获std::exception&
是一个好主意,const char*
也是如此。
C ++ 11使您能够通过catch(...)
在一定程度上检查std::current_exception
中的异常。请参见https://en.cppreference.com/w/cpp/error/current_exception。
关于c++ - 我可以使用try catch语句来捕获任何错误而不是特定错误吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59048774/