C++ 如何捕获未知异常并打印它
我有一些程序,每次我运行它时,它都会抛出异常,我不知道如何检查它到底抛出了什么,所以我的问题是,是否可以捕获异常并打印它(我找到了抛出异常的行)提前感谢如果它源自C++ 如何捕获未知异常并打印它,c++,exception,C++,Exception,我有一些程序,每次我运行它时,它都会抛出异常,我不知道如何检查它到底抛出了什么,所以我的问题是,是否可以捕获异常并打印它(我找到了抛出异常的行)提前感谢如果它源自std::exception您可以通过引用捕获: try { // code that could cause exception } catch (const std::exception &exc) { // catch anything thrown within try block that derive
std::exception
您可以通过引用捕获:
try
{
// code that could cause exception
}
catch (const std::exception &exc)
{
// catch anything thrown within try block that derives from std::exception
std::cerr << exc.what();
}
但是,除了这个例外,你不能做任何事情。先按照塞缪尔·克拉奇科(R Samuel Klatchko)的建议试试。如果这不起作用,那么还有其他可能的帮助: a) 如果调试器支持,请在异常类型(已处理或未处理)上放置断点
b) 在某些系统上,编译器在执行throw语句时生成对(未记录的?)函数的调用。要了解系统的功能,请编写一个简单的hello world程序,用于抛出和捕获异常。启动调试器并在exceptions构造函数中放置断点,然后查看从何处调用它。caling函数可能类似于_throw()。然后,使用要作为调试对象进行调查的程序再次启动调试器。将断点放在上面提到的函数上(uuu throw或其他什么),然后运行程序。抛出异常时,调试器将停止,您可以立即找到原因。如果您将ABI用于gcc或CLANG,则可以知道未知的异常类型。但它是非标准溶液 看这里 在C++11中,您有: 来自站点的示例代码:
#include <iostream>
#include <string>
#include <exception>
#include <stdexcept>
void handle_eptr(std::exception_ptr eptr) // passing by value is ok
{
try {
if (eptr) {
std::rethrow_exception(eptr);
}
} catch(const std::exception& e) {
std::cout << "Caught exception \"" << e.what() << "\"\n";
}
}
int main()
{
std::exception_ptr eptr;
try {
std::string().at(1); // this generates an std::out_of_range
} catch(...) {
eptr = std::current_exception(); // capture
}
handle_eptr(eptr);
} // destructor for std::out_of_range called here, when the eptr is destructed
#包括
#包括
#包括
#包括
void handle_eptr(std::exception_ptr eptr)//通过值传递是正常的
{
试一试{
如果(eptr){
std::返回异常(eptr);
}
}捕获(const std::exception&e){
std::cout受Dawid Drozd启发回答:
#include <exception>
try
{
// The code that could throw
}
catch(...)
{
auto expPtr = std::current_exception();
try
{
if(expPtr) std::rethrow_exception(expPtr);
}
catch(const std::exception& e) //it would not work if you pass by value
{
std::cout << e.what();
}
}
#包括
尝试
{
//可能引发
}
捕获(…)
{
auto expPtr=std::current_exception();
尝试
{
if(expPtr)std::rethrow_异常(expPtr);
}
catch(const std::exception&e)//如果按值传递,它将不起作用
{
标准::cout受哈马尼启发回答:
#include <iostream>
#include <string>
#include <exception>
#include <stdexcept>
int main()
{
try
{
// Your code
}
catch (...)
{
try
{
std::exception_ptr curr_excp;
if (curr_excp = std::current_exception())
{
std::rethrow_exception(curr_excp);
}
}
catch (const std::exception& e)
{
std::cout << e.what();
}
}
}
#包括
#包括
#包括
#包括
int main()
{
尝试
{
//你的代码
}
捕获(…)
{
尝试
{
std::例外情况(当前excp);
if(curr\u excp=std::current\u exception())
{
std::返回异常(当前excp);
}
}
捕获(const std::exception&e)
{
std::cout@R Samuel Klatchko:非常感谢,还有一个问题,我可以用你的方法检查new和delete的异常吗?@helloWorld-是的,这将捕获从new
,delete
以及它们调用的任何构造函数或析构函数引发的异常(前提是new
或delete
语句在try
块中)。如果异常不是从std::exception派生出来的,我该如何解决我的问题?@javapowered您是否碰巧看到下面Gregory81的答案(在您的评论后添加)这是没有帮助的,它仍然只处理std::exception。如果抛出类似char*的内容,这将没有帮助。Dawid Drozd。您回答的评论是用事实来表达的,更不用说礼貌,并且用技术解释来支持所做的陈述。另一方面,您的评论……不是也不是。仍然只处理std::异常
。
#include <iostream>
#include <string>
#include <exception>
#include <stdexcept>
int main()
{
try
{
// Your code
}
catch (...)
{
try
{
std::exception_ptr curr_excp;
if (curr_excp = std::current_exception())
{
std::rethrow_exception(curr_excp);
}
}
catch (const std::exception& e)
{
std::cout << e.what();
}
}
}