gpt4 book ai didi

java - 为什么不打印异常?

转载 作者:太空宇宙 更新时间:2023-11-03 23:33:09 28 4
gpt4 key购买 nike

下面是java代码,旁边是c代码:

package Package;

public class CatchThrow {

private native void doit() throws IllegalArgumentException;

private void callBack() throws NullPointerException {
System.out.println("In call-back !");
throw new NullPointerException("CatchThrow.callBack");
}

public static void main(String args[]) {
CatchThrow c = new CatchThrow();
try {
c.doit();
} catch(Exception exc) {
System.out.println("\nIn Java : \n\t" + exc);
}
}

static {
System.loadLibrary("CatchThrow");
}
}

C 代码:

#include "stdio.h"
#include "Package_CatchThrow.h"

void Java_Package_CatchThrow_doit
(JNIEnv *env, jobject obj) {
jthrowable exc;
jclass cls = (*env)->GetObjectClass(env,obj);
jmethodID mid = (*env)->GetMethodID(env,cls,"callBack","()V");
if(mid == NULL) {
return;
}
(*env)->CallVoidMethod(env,obj,mid); // call the java method that throws NullPointerException
printf("After the call to Call-Back !");
exc = (*env)->ExceptionOccurred(env);
if(exc) {
jclass newExcCls;
printf("\n");
//(*env)->ExceptionDescribe(env); ----> NOTE THE COMMENTED STATEMENT
//(*env)->ExceptionClear(env); -----> NOTE THE COMMENTED STATEMENT
newExcCls = (*env)->FindClass(env,"java/lang/IllegalArgumentException");
if(newExcCls == NULL) {
return;
}
(*env)->ThrowNew(env,newExcCls,"thrown from c code");
}

当我构建并运行上面的程序时,我得到以下输出:

In call-back !
After the call to Call-Back !

In Java :
java.lang.IllegalArgumentException: thrown from c code

从输出来看: C 代码调用 java 函数callBack。那里打印了第一个语句。调用后返回调用旁边的语句,即 After the call-Back ! 被打印出来。但是 java 函数 callBack 中的语句 throw new NullPointerException("CatchThrow.callBack"); 发生了什么?为什么不打印异常?

但是如果我从声明中删除评论

(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);

评论,我得到了想要的输出:

In call-back !
After the call to Call-Back !
Exception in thread "main" java.lang.NullPointerException: CatchThrow.callBack
at Package.CatchThrow.callBack(CatchThrow.java:14)
at Package.CatchThrow.doit(Native Method)
at Package.CatchThrow.main(CatchThrow.java:20)

In Java :
java.lang.IllegalArgumentException: thrown from c code

这是为什么?

这两个语句的作用是什么?为什么在没有这 2 个语句的情况下不打印异常?

最佳答案

您的 new NullPointerException("CatchThrow.callBack"); 永远不会触及 Java 运行时,并且完全在 JNI 空间中处理。

因此,除非您调用 JNI ExceptionDescribe 函数,否则不会打印任何异常详细信息:

(*env)->ExceptionDescribe(env); 

您不能从 JNI 返回多个异常,因此返回到 Java 运行时中的 try/catch block 的唯一异常信息是您稍后创建的异常信息:

(*env)->ThrowNew(env,newExcCls,"thrown from c code"); 

A pending exception raised through the JNI (by calling ThrowNew, for example) does not immediately disrupt the native method execution. This is different from how exceptions behave in the Java programming language. When an exception is thrown in the Java programming language, the virtual machine automatically transfers the control flow to the nearest enclosing try/catch statement that matches the exception type. The virtual machine then clears the pending exception and executes the exception handler. In contrast, JNI programmers must explicitly implement the control flow after an exception has occurred.

来自JNI documentation on exceptions

关于java - 为什么不打印异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10511968/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com