Search code examples
androidcrashjava-native-interfacetry-catchthrow

Try Catch in Android native library fails


When I perform a throw command in my native library, my Android application crashes. Does Android not support these calls? It never reaches the catch.

try
{
    __android_log_print(ANDROID_LOG_ERROR, "nativeLib", "throw");
    throw;
}
catch (...) {
    __android_log_print(ANDROID_LOG_ERROR, "nativeLib", "catch");
}

I recently switched from gnustl_shared to c++_shared, I'm not sure if this has anything to do with my problem.

Developer guides say that gnustl_shared has exceptions enabled by default, but c++_shared does not. I have included the -fexceptions flag as describe in the guide. https://developer.android.com/ndk/guides/cpp-support.html


Solution

  • It looks like this is a known issue:

    https://developer.android.com/ndk/guides/cpp-support.html

    Compatibility

    The NDK's libc++ is not stable. Not all the tests pass, and the test suite is not comprehensive. Some known issues are:

    •Using c++_shared on ARM can crash when an exception is thrown.

    •Support for wchar_t and the locale APIs is limited.