C ++捕获所有异常 [英] C++ catching all exceptions

查看:215
本文介绍了C ++捕获所有异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有与c ++相当的Java

Is there a c++ equivalent of Java's

try {
    ...
}
catch (Throwable t) {
    ...
}

I我试图调试Java / jni代码调用本机Windows函数和虚拟机不断崩溃。本机代码在单元测试中显得很好,只有通过jni调用时才会崩溃。一般的异常捕获机制将非常有用。

I am trying to debug Java/jni code that calls native windows functions and the virtual machine keeps crashing. The native code appears fine in unit testing and only seems to crash when called through jni. A generic exception catching mechanism would prove extremely useful.

推荐答案

try{
    // ...
} catch (...) {
    // ...
}

将捕获所有C ++异常,但它应该被认为是坏的设计。你可以使用c ++ 11的新的current_exception机制,但如果你没有能力使用c ++ 11(遗留代码系统需要重写),那么你没有命名异常指针用于获取消息或名称。您可能需要为可以捕获的各种异常添加单独的catch子句,并且只捕获底部的所有内容以记录意外异常。例如:

will catch all C++ exceptions, but it should be considered bad design. You can use c++11's new current_exception mechanism, but if you don't have the ability to use c++11 (legacy code systems requiring a rewrite), then you have no named exception pointer to use to get a message or name. You may want to add separate catch clauses for the various exceptions you can catch, and only catch everything at the bottom to record an unexpected exception. E.g.:

try{
    // ...
} catch (const std::exception& ex) {
    // ...
} catch (const std::string& ex) {
    // ...
} catch (...) {
    // ...
}

这篇关于C ++捕获所有异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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