What Happens When I Throw a C++ Exception from a Native Java Method

What happens when I throw a C++ exception from a native Java method?

Within the JNI literature, the word exception appears to be used exclusively to refer to Java exceptions. Unexpected events in native code are referred to as programming errors. JNI explicitly does not require JVMs to check for programming errors. If a programming error occurs, behavior is undefined. Different JVMs may behave differently.

It's the native code's responsibility to translate all programming errors into either return codes or Java exceptions. Java exceptions don't get thrown immediately from native code. They can be pending, only thrown once the native code returns to the Java caller. The native code can check for pending exceptions with ExceptionOccurred and clear them with ExceptionClear.

Exception while calling java method from cpp using JNI

The initialization of MyManager throws a NullPointerException, which probably prevents determining the method id, so you might want to look at that. Note that not only the class initializer could throw this but also initialization of any static fields. You could put all initialization into the class initializer and then debug from there, something there must be null.

JNI error while calling a C subroutine

You need to have your library explicitly set on your path.

It may be the case that the flags you are using there aren't quite right. Try this:

gcc -Wall -D_JNI_IMPLEMENTATION_ -Wl,--kill-at \
-I[Java_HOME]/include -I[Java_HOME]/include/win32 \
-shared -o Sample1.dll Sample1.c

From MinGW GCC site.

An exception happened in constructor of class com.mgr.ui.viewmodels.MainViewModel

Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void androidx.compose.runtime.MutableState.setValue(java.lang.Object)' on a null object reference

You can try moving your init {} block to be after you declare displayMenu. I am not confident that this will work, but it is worth a try.

If that does not help, then you may need to postpone your use of the SavedStateHandle until sometime when the MutableState is ready for use.

ClassNotFoundException I didn't succeed to catch

You only catch exceptions you list in catch(...). The stack trace shows the exception in the main method is an NoClassDefFoundError.

To solve it replace

catch (OWLOntologyCreationException|OWLOntologyStorageException|ClassNotFoundException e) {
System.out.println(name + " has not been saved into RDF/XML;");
System.out.println(e.getMessage());
}

By (here you catch all exceptions)

catch (Exception e) {
System.out.println(name + " has not been saved into RDF/XML;");
System.out.println(e.getMessage());
}

Or by

catch (OWLOntologyCreationException|OWLOntologyStorageException
|ClassNotFoundException|NoClassDefFoundErrore) {
System.out.println(name + " has not been saved into RDF/XML;");
System.out.println(e.getMessage());
}


Related Topics



Leave a reply



Submit