接住从本地code抛出运行在Android上的异常抛出、接住、异常、code

2023-09-05 05:33:58 作者:共觅.

我目前工作的项目,需要我code了一个跨平台的程序执行了Android的部分。

The project I'm currently working on requires me to code up the android portion of a cross platform program implementation.

功能性的核心集是通过建并包括在我的应用程序的Andr​​oid NDK 。我发现任何异常/崩溃而发生在本土code仅现在又在最好的报道。当错误发生时,我得到以下行为之一:

A core set of functionality is built and included in my app through android-ndk. I've found that any exception/crash which happens in the native code is only reported now and again at best. When an error occurs I get one of the following behaviours:

在堆栈跟踪/内存转储发生,并写入日志文件。该计划将消失(但并未说明在设备上,为什么突然应用程序不再有)。 在没有堆栈跟踪/转储或其他指示,因为本机code已经崩溃。该程序会消失。 与Java code坠毁一个 NullPointerException异常(通常在每本土code异常一样的地方是一个巨大的疼痛)。通常害我花相当长的时间试图调试为什么Java的code已经抛出一个错误,却发现了Java code是罚款和放大器;本机code错误已被完全掩盖。 A stacktrace / memory dump occurs and is written to the log file. The program disappears (no indication is given on the device as to why suddenly the app is no longer there). No stacktrace / dump or other indication is given that the native code has crashed. The program disappears. The java code crashes with a NullPointerException (usually in the same place per native code exception which is a massive pain). Usually causing me to spend quite a while trying to debug why the Java code has thrown an error only to discover the Java code is fine & the native code error has been entirely masked.

我似乎无法找到任何方式来隔离我的code对发生在本土code错误。 try / catch语句被响亮地忽略。除了当我的code是指为罪魁祸首,我不甚至有机会警告不是发生了错误的用户。

I can't seem to find any way to "insulate" my code against errors which occur in native code. Try/catch statements are resoundingly ignored. Apart from when my code is fingered as the culprit I don't even get a chance to warn the user than an error has occurred.

是否有人可以帮助我,如何向本地崩溃code的情况作出反应?

Can someone please help me out as to how to respond to the situation of crashing native code?

推荐答案

我曾经有过同样的问题,如果你把一个C是在Android的(内部的任何虚拟机一般在执行本机code)真++例外,这个人是没有被捕获,虚拟机模(如果我理解正确的,我认为这是你的问题)。我采用的方法是赶在C ++中的任何异常并抛出使用JNI的Java异常代替。 C时的下一个$ C $是我的解决方案的一个简化的例子。首先,你有一个JNI方法,捕捉C ++异常,然后在try子句中的Java异常的注解。

I used to have the same problem, it is true that in android (inside any VM in general when executing native code) if you throw a C++ exception and this one is not caught, the VM dies (If I understood correctly, I think it is your problem). The solution I adopted was to catch any exception in C++ and throw a java exception instead of using JNI. The next code it is a simplified example of my solution. First of all you have a JNI method that catches a C++ exception and then in the try clause the Java exception is annotated.

JNIEXPORT void JNICALL Java_com_MyClass_foo (JNIEnv *env, jobject o,jstring param)
{
    try
    {
        // Your Stuff
        ...
    }
    // You can catch std::exception for more generic error handling
    catch (MyCxxException e)
    {
        throwJavaException (env, e.what());
    }
}


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

    if (NULL == c)
    {
        //B plan: null pointer ...
        c = env->FindClass("java/lang/NullPointerException");
    }

    env->ThrowNew(c, msg);
}

请注意,一个ThrowNew后,本地方法不会突然自动终止。即,控制流程返回到本机的方法,以及新的异常挂起在这一点上。后您的JNI方法完成的异常将被抛出。

Note that after a ThrowNew, the native method does not abruptly terminate automatically. That is, control flow returns to your native method, and the new exception is pending at this point. The exception will be thrown after your JNI method is finished.

我希望这是你正在寻找的解决方案。

I hope it was the solution you are looking for.