Checking errors and handling exceptions in JNI