Catching JNI exception

1,930 views
Skip to first unread message

HanWriting

unread,
May 3, 2010, 4:50:38 PM5/3/10
to android-ndk
I understand why C++ exceptions weren't included for efficiency
reason.
But what about catching a generic exception under the Java?
Wouldn't it be nice to have something like JNIException for us to
catch?


HanWriting
(CJK+Eng Handwriting Recognition)

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.

Angus Lees

unread,
May 4, 2010, 3:45:46 AM5/4/10
to andro...@googlegroups.com
On Tue, May 4, 2010 at 06:50, HanWriting <honsoha...@gmail.com> wrote:
I understand why C++ exceptions weren't included for efficiency
reason.
But what about catching a generic exception under the Java?
Wouldn't it be nice to have something like JNIException for us to
catch?

As I understand it, the cost with C++ exceptions is in having the compiler generate all the potential stack unwind codepaths.  Actually catching the C++ exception in C++ is the easy bit.


If it helps your error recovery codepaths, you can easily throw a custom Java exception from C++ using something like:
 jclass cls = env->FindClass("my/package/JNIException");
 env->ThrowNew(cls, "Error in JNI code!");

You can also do C pre-exception coding tricks like setjmp/longjmp, but you really want to understand what that's all about before trying it.

 - Gus

HanWriting

unread,
May 4, 2010, 10:39:59 AM5/4/10
to android-ndk


> If it helps your error recovery codepaths, you can easily throw a custom
> Java exception from C++ using something like:
>  jclass cls = env->FindClass("my/package/JNIException");
>  env->ThrowNew(cls, "Error in JNI code!");

I know I can do it manually but that isn't what I want.
What I want is the virtual machine should automatically throw an
exception when something wrong happens in JNI calls.
Currently it simply close the whole app give us no chance.

HanWriting
(CJK+Eng Handwriting Recognition)

Angus Lees

unread,
May 4, 2010, 6:51:48 PM5/4/10
to andro...@googlegroups.com
On Wed, May 5, 2010 at 00:39, HanWriting <honsoha...@gmail.com> wrote:


> If it helps your error recovery codepaths, you can easily throw a custom
> Java exception from C++ using something like:
>  jclass cls = env->FindClass("my/package/JNIException");
>  env->ThrowNew(cls, "Error in JNI code!");

I know I can do it manually but that isn't what I want.
What I want is the virtual machine should automatically throw an
exception when something wrong happens in JNI calls.
Currently it simply close the whole app give us no chance.

Coding errors in C and C++ can corrupt memory anywhere in the current process.  There is no way for a process to reliably recover from (for example) a segfault.  JNI code runs in the same process as the Java virtual machine and the Java code further up the call stack (that's the point of JNI).

You *can* reliably get *another* process to notice that the first one has crashed and do something about it, but the overheads in forking a new process and then marshalling function calls across to the other process are considerable.  The closest thing to this in Android is using a service, and then ensuring that the service runs in its own process (and not in the process of the caller, which is a common Android optimisation).  If the other (service) process crashes, then the caller is able to catch an error and continue - the downside is that all calls to that service will be more expensive than usual.  See http://developer.android.com/guide/topics/fundamentals.html#procthread and the android:process attribute on the AndroidManifest <service> tag if you want to go that way.

The unfortunate reality of C/C++ is that developing native code involves more explicit error recovery codepaths and sudden crashes when you get it wrong.

 - Gus
Reply all
Reply to author
Forward
0 new messages