Android NDK R5 and support of C++ exception

android-ndk, c++, exception

Solution

It turns out that exceptions work but only if the exception are inherited from std::exception. In my case the exception hierarchy did not always include std::exception which broke the catch/throw. Curiously the throwing strings as exceptions works when compiled for x86/Mac OS. I have fixed my problem by modifying the exceptions I use.

Problem

I am trying to use the NDK 5 full C++ gnustl: The `CPLUSPLUS-SUPPORT.html` states: The NDK toolchain supports C++ exceptions, since NDK r5, however all C++ sources are compiled with -fno-exceptions support by default, for compatibility reasons with previous releases. To enable it, use the '-fexceptions' C++ compiler flag. This can be done by adding the following to every module definition in your Android.mk: ``` LOCAL_CPPFLAGS += -fexceptions ``` More simply, add a single line to your Application.mk, the setting will automatically apply to all your project's NDK modules: ``` APP_CPPFLAGS += -fexceptions ``` `sources/cxx-stl/gnu-libstdc++/README` states: This directory contains the headers and prebuilt binaries for the GNU libstdc++-v3 C++ Standard Template Library implementation. These are generated from the toolchain sources by the rebuild-all-prebuilt.sh script under build/tools. To use it, define APP_STL to 'gnustl_static' in your Application.mk. See docs/CPLUSPLUS-SUPPORT.html for more details. This implementation fully supports C++ exceptions and RTTI. But all attempts using exceptions fail. An alternative NDK exists on http://www.crystax.net/android/ndk-r4.php. Using the the hello-jni example from that NDK does not work. Compliation with NDK 5 works after creating an `Application.xml` with ``` APP_STL := gnustl_static ``` Setting APP_STL to `gnustl_static` also automatically enables `-frtti` and `-fexceptions`. But it dies the same horrific death as my own experiments. I have managed to get a minimal example of code that is crashing for me: ``` try { __android_log_write(ANDROID_LOG_DEBUG,"foobar","trhown!"); throw "Wrong object type."; } catch (char* b) { __android_log_write(ANDROID_LOG_DEBUG,"foobar","catched!"); } ``` Am I am missing something or is the statement in the `README` and `CPLUSPLUS-SUPPORT.html` just plain wrong?

Original source