我正在用c ++编写一个java本机库,并在本机lib本身中使用异常处理,但是一旦抛出异常,库就会崩溃.这是我的简单测试程序,当我从Java测试中调用它时,它会在抛出异常时立即崩溃.catch块无法正常工作.我想念的任何想法.谢谢.
#include "Test.h"
#include <iostream>
JNIEXPORT void JNICALL Java_Test_helloWorld(JNIEnv *, jobject)
{
std::cout<<"Hello World";
try {
throw 1;
}
catch(int )
{
std::cout<<" catch int block"<<std::endl;
}
catch(...)
{
std::cout<<" catch block"<<std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
编译和链接:
g++ -m64 -fPIC -fexceptions -c test.cpp
g++ -shared -m64 -Wl,-soname,libtest.so -Wl,-shared-libgcc test.o -o libtest.so
$ java -d64 -Djava.library.path=/home/vkumar/projects/test -cp $CLASSPATH Test
terminate called after throwing an instance of 'int'
terminate called recursively
Hello World^CAbort (core dumped)
Run Code Online (Sandbox Code Playgroud)