Skip to content

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

@mojo2012

Description

@mojo2012

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions