我是JNI的新手。我正在尝试Cpp中的异常处理。对于找不到类的异常,我可以按以下方式处理异常
jclass some=env->FindClass("java/some/class"); jboolean flag = env->ExceptionCheck(); if (flag) { env->ExceptionClear(); printf("Exception in native method"); jclass jcls =env->FindClass("java/lang/ClassNotFoundException"); env->ThrowNew(jcls, "Exception in Native Method"); }
但是对于methodid,如果我输入了错误的methodName,它不会引发异常。
jmethodID mId = env->GetMethodID( someClass, "wrongmethodName", "()V"); jboolean flag = env->ExceptionCheck(); if (flag) { env->ExceptionClear(); printf("Exception in native method"); jclass jcls =env->FindClass("java/lang/Exception"); env->ThrowNew(jcls, "Exception in Native Method"); }
相反,JVM崩溃并生成包含以下内容的日志文件为hs_err_pidxxx.txt
# # A fatal error has been detected by the Java Runtime Environment: # # EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x63c7e45b, pid=30652, tid=0x00009098 # # JRE version: Java(TM) SE Runtime Environment (8.0_181-b13) (build 1.8.0_181-b13) # Java VM: Java HotSpot(TM) Client VM (25.181-b13 mixed mode, sharing windows-x86 ) # Problematic frame: # V [jvm.dll+0xde45b] # # Failed to write core dump. Minidumps are not enabled by default on client versions of Windows # # If you would like to submit a bug report, please visit: # http://bugreport.java.com/bugreport/crash.jsp # --------------- T H R E A D --------------- Current thread (0x193d7800): JavaThread "http-nio-8080-exec-2" daemon [_thread_in_vm, id=37016, stack(0x19cb0000,0x19d00000)] siginfo: ExceptionCode=0xc0000005, reading address 0x00000000 Registers: EAX=0x00000000, EBX=0x193d7940, ECX=0x193d7800, EDX=0x0000062d ESP=0x19cff040, EBP=0x19cff088, ESI=0x193d7800, EDI=0x00000000 EIP=0x63c7e45b, EFLAGS=0x00010246 Top of Stack: (sp=0x19cff040) 0x19cff040: 193d7800 18dd0880 18dd0888 63e87cf4 0x19cff050: 176397f0 193d7800 00000000 0000000e 0x19cff060: 19cfefdc 19cff0d4 0000000e 193d7800 0x19cff070: 00000007 18dd0888 193d7800 17e1cbc4 0x19cff080: 0000062d 63e87bc8 19cff0b8 6af82dd8 0x19cff090: 193d7940 18a00b14 00000000 19cff0cc 0x19cff0a0: 74ca2fcb 74cae600 19cff0cc 19cff0cc 0x19cff0b0: 00000000 19cff0c8 19cff238 6af81b74 Instructions: (pc=0x63c7e45b) 0x63c7e43b: f2 ff 83 c4 08 83 7e 04 00 89 75 f0 c7 45 f4 00 0x63c7e44b: 00 00 00 74 08 8d 4d f0 e8 08 c3 0c 00 8b 7d 10 0x63c7e45b: 8b 07 c7 45 e0 04 00 00 00 8b 40 04 0f b7 48 1c 0x63c7e46b: 8b 50 08 8b 44 8a 2c 50 8d 4d c4 e8 b5 5e 09 00 Register to memory mapping: EAX=0x00000000 is an unknown value EBX=0x193d7940 is an unknown value ECX=0x193d7800 is a thread EDX=0x0000062d is an unknown value ESP=0x19cff040 is pointing into the stack for thread: 0x193d7800 EBP=0x19cff088 is pointing into the stack for thread: 0x193d7800 ESI=0x193d7800 is a thread EDI=0x00000000 is an unknown value Stack: [0x19cb0000,0x19d00000], sp=0x19cff040, free space=316k Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code) V [jvm.dll+0xde45b] C [crud.dll+0x2dd8] C [crud.dll+0x1b74] j NativeService.view(LUser;)Ljava/util/ArrayList;+0
谁能建议我一种处理异常的方法,而不是JVM崩溃和错误日志的生成。一堆谢谢。