recipe № 019

Throwing exception from C code

Sometimes you need to make sure that error is properly reported inside JVM. If you face some real issues inside native code (especially ones that you can not recover from) it’s good to provide some information back to JVM. Sometimes, the best approach is to throw exception. You can easily do that using ThrowNew function.


In this sample, main part of JNI code is responsible for creating and throwing exception back to Java.

All we have to do is simply throwing object of given class together with message we want to send back to Java code. In fact, that’s all we need to do here.

If you want to run this code, simply clone the repo and run the sample.