我应该以哪种方式检查JNI中的异常? [英] Which way should I check exceptions in JNI?

查看:85
本文介绍了我应该以哪种方式检查JNI中的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道,JNI中的许多方法都可能导致异常,并且在导致异常的可能导致JVM崩溃的情况下无法调用.我们不仅可以使用ExceptionOccurred()或ExceptionCheck()来确定是否发生异常,还可以简单地检查不等于NULL或0的值.最佳方法是什么?

As we know, many methods in JNI may cause exception and cannot be called after exception caused that may crash the JVM. We can not only use ExceptionOccurred() or ExceptionCheck() to determine whether exception occurred or not, but also can simply check something not equal to NULL or 0. Which way is the best practice?

推荐答案

当您只要求最佳实践"时,答案非常简单:

As you are only asking for "best practice", the answer is very straightforward:

    调用可能会引发异常的JNI函数时,
  1. 始终调用ExceptionCheckExceptionOccurred.
  2. 始终检查JNI函数的返回值
  1. always call ExceptionCheck or ExceptionOccurred when calling JNI functions which may raise exceptions.
  2. always check return values from JNI functions

在每次JNI调用之后(最好在专用功能中)调用ExceptionOccurred,您可以获取有关异常的一些信息以进行记录,还可以调用ExceptionClear.

Calling ExceptionOccurred after each JNI call, preferably in a dedicated function, allows you to get some information on the exception, for logging, and also allows you to call ExceptionClear.

使专用函数返回void,并使用JNI函数的返回值来区分正常路径和错误路径.

Make the dedicated function return void, and use the return values from the JNI functions to distinguish between normal and error paths.

这篇关于我应该以哪种方式检查JNI中的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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