从catch(...)捕获的异常获取一些信息? [英] Get some information from exception caught with catch(...)?

查看:1353
本文介绍了从catch(...)捕获的异常获取一些信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个try catch子句,最外面的 catch(...)从来没有发生到现在。在一些更改后,某处有一个异常,我不处理其他情况下抛出。有没有办法获得至少一些关于异常的信息,即使我捕获它(...)

I have a try catch clause where the outermost catch(...) never happened until now. After some changes, somewhere an exception which I don't handle with the other cases is thrown. Is there a way to get at least some information about the exception even though I catch it with (...)?

catch (const cone::BeginnersLibException& ex) 
{
    // handle the exception
}
catch (const std::exception& ex) 
{
    // handle std exception
} 
catch (...) 
{
    log("Unknown exception caught.");
    // How can I get more information about this exception?
}






适用于我的片段:


here a code snippet that works for me:

#include <cxxabi.h>

// more code here 
} catch (...) {
    std::string exName(abi::__cxa_current_exception_type()->name());
    std::cout<<"unknown exception: "<< exName <<std::endl;
    throw;
}


推荐答案

或另一个调试器。当任何异常被抛出时,告诉调试器停止(在gdb中,命令是滑稽的 catch throw )。

You can do this using gdb or another debugger. Tell the debugger to stop when any exception is throw (in gdb the command is hilariously catch throw). Then you will see not only the type of the exception, but where exactly it is coming from.

另一个想法是注释掉 catch(),然后你会看到异常的类型, ...),并让您的运行时终止您的应用程序,并希望告诉您更多关于异常。

Another idea is to comment out the catch (...) and let your runtime terminate your application and hopefully tell you more about the exception.

,你应该尝试用从 std :: exception 派生的东西替换或扩充它。必须 catch(...)根本不是很好。

Once you figure out what the exception is, you should try to replace or augment it with something that does derive from std::exception. Having to catch (...) at all is not great.

如果使用GCC或Clang,也可以尝试 __ cxa_current_exception_type() - > name()来获取当前异常类型的名称。

If you use GCC or Clang you can also try __cxa_current_exception_type()->name() to get the name of the current exception type.

这篇关于从catch(...)捕获的异常获取一些信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆