gpt4 book ai didi

c++ - 我可以使用 try catch 语句来捕获任何错误而不是具体错误吗?

转载 作者:行者123 更新时间:2023-11-30 20:44:41 25 4
gpt4 key购买 nike

 try{
//some function
}catch(...){
//write information to a file including this line number
}

--

是什么阻止我这样做而不是指定空指针异常?无论如何,程序都会崩溃,所以如果有什么我没有考虑到的话,我不妨捕获所有错误?

最佳答案

每个程序错误都会被catch(...)捕获,这是一个神话。特别是空指针取消引用的行为未定义,因此根本无法保证它会被捕获。

如果您想要一个特别通用的 catch 站点,那么捕获 std::exception& 是一个好主意,const char* 也是如此。

C++11 使您能够通过 std::current_exception 在一定程度上检查 catch(...) 中的异常。请参阅https://en.cppreference.com/w/cpp/error/current_exception .

关于c++ - 我可以使用 try catch 语句来捕获任何错误而不是具体错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59048774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com