Don't discard thrown exception#1651
Open
xxDark wants to merge 1 commit intojava-native-access:masterfrom
Open
Conversation
Member
matthiasblaesing
left a comment
There was a problem hiding this comment.
Sorry for the late review. In general the idea makes sense. I left an inline comment, that shows how a simpler version could look from my POV.
What is crucially missing is a unittest, that verifies that this works. Please add a demonstrator to validate.
Comment on lines
426
to
469
| if (cls != NULL) { /* Otherwise an exception has already been thrown */ | ||
| if (thrown != NULL) { | ||
| /* Attach thrown exception if possible. */ | ||
| /* do_throw_clean is an exit path that will delete 'thrown' if we can't do that. */ | ||
| jmethodID init = (*env)->GetMethodID(env, cls, "<init>", "(Ljava/lang/String;)V"); | ||
| if (init == NULL) { | ||
| goto do_throw_clean; | ||
| } | ||
| jmethodID initCause = (*env)->GetMethodID(env, cls, "initCause", "(Ljava/lang/Throwable;)Ljava/lang/Throwable;"); | ||
| if (initCause == NULL) { | ||
| goto do_throw_clean; | ||
| } | ||
| jobject str = (*env)->NewStringUTF(env, msg); | ||
| if (str == NULL) { | ||
| goto do_throw_clean; | ||
| } | ||
| jthrowable t = (jthrowable) (*env)->NewObject(env, cls, init, str); | ||
| (*env)->DeleteLocalRef(env, str); | ||
| if (t == NULL) { | ||
| goto do_throw_clean; | ||
| } | ||
| jobject ret = (*env)->CallObjectMethod(env, t, initCause, thrown); | ||
| if (ret == NULL) { | ||
| goto do_throw_clean; | ||
| } | ||
| (*env)->DeleteLocalRef(env, ret); | ||
| (*env)->Throw(env, t); | ||
| goto clean_cls; | ||
| } else { | ||
| goto do_throw; | ||
| } | ||
| /* Deletes thrown exception reference and clears the exception. */ | ||
| do_throw_clean: | ||
| (*env)->DeleteLocalRef(env, thrown); | ||
| (*env)->ExceptionClear(env); | ||
| do_throw: | ||
| (*env)->ThrowNew(env, cls, msg); | ||
|
|
||
| clean_cls: | ||
| /* It's a good practice to clean up the local references. */ | ||
| (*env)->DeleteLocalRef(env, cls); | ||
| } else if (thrown != NULL) { | ||
| (*env)->DeleteLocalRef(env, thrown); | ||
| } |
Member
There was a problem hiding this comment.
I think the cases are to complex and can be simplified to:
Suggested change
| if (cls != NULL) { /* Otherwise an exception has already been thrown */ | |
| if (thrown != NULL) { | |
| /* Attach thrown exception if possible. */ | |
| /* do_throw_clean is an exit path that will delete 'thrown' if we can't do that. */ | |
| jmethodID init = (*env)->GetMethodID(env, cls, "<init>", "(Ljava/lang/String;)V"); | |
| if (init == NULL) { | |
| goto do_throw_clean; | |
| } | |
| jmethodID initCause = (*env)->GetMethodID(env, cls, "initCause", "(Ljava/lang/Throwable;)Ljava/lang/Throwable;"); | |
| if (initCause == NULL) { | |
| goto do_throw_clean; | |
| } | |
| jobject str = (*env)->NewStringUTF(env, msg); | |
| if (str == NULL) { | |
| goto do_throw_clean; | |
| } | |
| jthrowable t = (jthrowable) (*env)->NewObject(env, cls, init, str); | |
| (*env)->DeleteLocalRef(env, str); | |
| if (t == NULL) { | |
| goto do_throw_clean; | |
| } | |
| jobject ret = (*env)->CallObjectMethod(env, t, initCause, thrown); | |
| if (ret == NULL) { | |
| goto do_throw_clean; | |
| } | |
| (*env)->DeleteLocalRef(env, ret); | |
| (*env)->Throw(env, t); | |
| goto clean_cls; | |
| } else { | |
| goto do_throw; | |
| } | |
| /* Deletes thrown exception reference and clears the exception. */ | |
| do_throw_clean: | |
| (*env)->DeleteLocalRef(env, thrown); | |
| (*env)->ExceptionClear(env); | |
| do_throw: | |
| (*env)->ThrowNew(env, cls, msg); | |
| clean_cls: | |
| /* It's a good practice to clean up the local references. */ | |
| (*env)->DeleteLocalRef(env, cls); | |
| } else if (thrown != NULL) { | |
| (*env)->DeleteLocalRef(env, thrown); | |
| } | |
| if (cls != NULL) { /* Otherwise an exception has already been thrown */ | |
| jthrowable t = NULL; | |
| jmethodID init = (*env)->GetMethodID(env, cls, "<init>", "(Ljava/lang/String;)V"); | |
| jobject str = (*env)->NewStringUTF(env, msg); | |
| if (init != NULL && str != NULL) { | |
| t = (jthrowable) (*env)->NewObject(env, cls, init, str); | |
| } | |
| if (str != NULL) { | |
| (*env)->DeleteLocalRef(env, str); | |
| } | |
| if (thrown != NULL && t != NULL) { | |
| jmethodID initCause = (*env)->GetMethodID(env, cls, "initCause", "(Ljava/lang/Throwable;)Ljava/lang/Throwable;"); | |
| /* Attach thrown exception if possible. */ | |
| jobject ret = (*env)->CallObjectMethod(env, t, initCause, thrown); | |
| (*env)->DeleteLocalRef(env, ret); | |
| } | |
| (*env)->Throw(env, t); | |
| (*env)->DeleteLocalRef(env, cls); | |
| } | |
| if (thrown != NULL) { | |
| (*env)->DeleteLocalRef(env, thrown); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Attach pending exception as the cause in
throwByNameif possible. This change makes it easier to debug errors.