Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch all errors in native code and throw Java exceptions #8

Open
mojo2012 opened this issue Jun 3, 2019 · 2 comments
Open

Catch all errors in native code and throw Java exceptions #8

mojo2012 opened this issue Jun 3, 2019 · 2 comments

Comments

@mojo2012
Copy link
Collaborator

mojo2012 commented Jun 3, 2019

Hi there,

I'm trying to implement a generic error handling in the native library like this:

-(void) throwJavaException:(JNIEnv *)env withMessage: (const char *)msg
{
    // You can put your own exception here
    jclass c = (*env)->FindClass(env, "java/lang/RuntimeException");
    
    (*env)->ThrowNew(env, c, msg);
}

And wrapped all functions/methods/selectors in WLJavaProxy.m into this try catch:

@try
    {
         ....
    } @catch (NSException *e) {
        [self throwJavaException: env withMessage: [[e reason] UTF8String] ];
        NSLog(@"Exception: %@", e);
    }

The hope was that for any error that occurred in native code, the VM would not crash but rather show a RuntimeException.

Is this the correct approach to do this?
It would be nice to have such a thing to prevent crashes!

@shannah
Copy link
Owner

shannah commented Jun 3, 2019

That would be useful. Your approach looks fine. With anything JNI related, there may be some corner cases that make it more difficult, but you'll have to wait for those cases to present themselves.

@mojo2012
Copy link
Collaborator Author

mojo2012 commented Jun 3, 2019

I'll try to produce a test scenario and make a PR.
Btw, it would be awesome to have the library on maven central!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants