mitchdowd / jnipp

C++ wrapper for the Java Native Interface
MIT License
106 stars 29 forks source link

handle (class method field) Java Exception #28

Closed qtfreet00 closed 4 years ago

qtfreet00 commented 4 years ago
    try {
        jni::Class ctx("android/content/Context1");
    } catch (jni::NameResolutionException &e) {
        LOGE("the exception %s",e.what());
    }

in demo,this action will crash ,because java class Context1 can not find

add handleJavaExcption for getClass getxxxField getxxxMethod before throw std::exception

mitchdowd commented 4 years ago

I can't seem to reproduce this issue. I have a single-file project with the following code:

#include <iostream>
#include <jnipp.h>

int main()
{
    jni::Vm vm;

    try {
        jni::Class ctx("android/content/Context1");
    }
    catch (jni::NameResolutionException& e) {
        std::cout << "the exception " << e.what() << std::endl;
    }

    std::cin.get();
    return 0;
}

It is producing the following output, as expected:

the exception android/content/Context1

Can you give a more comprehensive example?

Note that the internal handleJavaExceptions() function in the source code is for raising InvocationException to indicate that an exeption was thrown in the Java stack that propagated up to the C++ code.

qtfreet00 commented 4 years ago

maybe i tested it on Android?

qtfreet00 commented 4 years ago

I changed findclass method like this

Class xx = env->FindClass(xxxx)
if(xx==nullptr){
        jthrowable exception = env->ExceptionOccurred();

        if (exception != nullptr) {
            env->ExceptionClear();
            throw NameResolutionException();
        }
}

it works well

mitchdowd commented 4 years ago

Aha, right you are. FindClass() raises a Java exception if it fails. I'll do a clear if this happens. Thanks for raising!