c++中有类似Java的吗
try {
...
}
catch (Throwable t) {
...
}
我试图调试Java/jni代码调用本机windows函数和虚拟机不断崩溃。本机代码在单元测试中表现良好,只有在通过jni调用时才会崩溃。一个通用的异常捕获机制将被证明是非常有用的。
c++中有类似Java的吗
try {
...
}
catch (Throwable t) {
...
}
我试图调试Java/jni代码调用本机windows函数和虚拟机不断崩溃。本机代码在单元测试中表现良好,只有在通过jni调用时才会崩溃。一个通用的异常捕获机制将被证明是非常有用的。
当前回答
可以这样写:
try
{
//.......
}
catch(...) // <<- catch all
{
//.......
}
但是这里有一个非常不明显的风险:您无法找到try块中抛出的错误的确切类型,因此当您确定无论异常类型是什么,程序都必须以catch块中定义的方式持续存在时,可以使用这种catch。
其他回答
如果你正在寻找特定于windows的解决方案,那么有结构化异常处理: https://learn.microsoft.com/en-us/cpp/cpp/try-except-statement
代码如下所示
__try
{
// code here may throw or make access violation
}
__except( EXCEPTION_EXECUTE_HANDLER )
{
// after exception code here, e.g. log the error
}
它不仅可以捕获c++异常,还可以捕获访问违规或其他系统异常。
可以这样写:
try
{
//.......
}
catch(...) // <<- catch all
{
//.......
}
但是这里有一个非常不明显的风险:您无法找到try块中抛出的错误的确切类型,因此当您确定无论异常类型是什么,程序都必须以catch块中定义的方式持续存在时,可以使用这种catch。
这是如何从catch(…)中反向工程异常类型,如果你需要(可能有用时,从第三方库捕获未知)使用GCC:
#include <iostream>
#include <exception>
#include <typeinfo>
#include <stdexcept>
int main()
{
try {
throw ...; // throw something
}
catch(...)
{
std::exception_ptr p = std::current_exception();
std::clog <<(p ? p.__cxa_exception_type()->name() : "null") << std::endl;
}
return 1;
}
如果你有能力使用Boost,你可以让你的捕捉部分更简单(在外部),并具有跨平台的潜力
catch (...)
{
std::clog << boost::current_exception_diagnostic_information() << std::endl;
}
嗯,如果你想捕获所有异常来创建一个小转储,例如…
有人做了Windows的工作。
参见http://www.codeproject.com/Articles/207464/Exception-Handling-in-Visual-Cplusplus 在这篇文章中,他解释了他如何发现如何捕获各种异常,并提供了有效的代码。
以下是你可以找到的清单:
SEH exception
terminate
unexpected
pure virtual method call
invalid parameter
new operator fault
SIGABR
SIGFPE
SIGILL
SIGINT
SIGSEGV
SIGTERM
Raised exception
C++ typed exception
以及用法: CCrashHandler ch; ch.SetProcessExceptionHandlers ();//为一个线程执行此操作 ch.SetThreadExceptionHandlers ();//每个thred
默认情况下,这会在当前目录(crashdump.dmp)中创建一个迷你转储。
Can you run your JNI-using Java application from a console window (launch it from a java command line) to see if there is any report of what may have been detected before the JVM was crashed. When running directly as a Java window application, you may be missing messages that would appear if you ran from a console window instead. Secondly, can you stub your JNI DLL implementation to show that methods in your DLL are being entered from JNI, you are returning properly, etc? Just in case the problem is with an incorrect use of one of the JNI-interface methods from the C++ code, have you verified that some simple JNI examples compile and work with your setup? I'm thinking in particular of using the JNI-interface methods for converting parameters to native C++ formats and turning function results into Java types. It is useful to stub those to make sure that the data conversions are working and you are not going haywire in the COM-like calls into the JNI interface. There are other things to check, but it is hard to suggest any without knowing more about what your native Java methods are and what the JNI implementation of them is trying to do. It is not clear that catching an exception from the C++ code level is related to your problem. (You can use the JNI interface to rethrow the exception as a Java one, but it is not clear from what you provide that this is going to help.)