mrousavy
mrousavy

Reputation: 1117

`std::rethrow_exception` not catchable with `catch (const std::exception&)` in Android NDK

I have an Android app that calls C++ code from Java. This is my C++ code:

try {
  throw std::runtime_error("Test exception");
} catch (...) {
  auto eptr = std::current_exception();
  try {
    std::rethrow_exception(eptr);
  } catch (const std::exception& e) {
    std::cout << "Caught std::exception: " << e.what() << std::endl;
  } catch (...) {
    std::cout << "Caught unknown exception" << std::endl;
  }
}

Why? How can the same code that only uses std lib produce different results? Does the Android STD/STL library not support exception_ptr?


More Info:

The native C++ library is built with build.gradle/CMake using the following configuration:

android {
  namespace "com.margelo.nitro"

  ndkVersion getExtOrDefault("ndkVersion")
  compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")

  defaultConfig {
    minSdkVersion getExtOrIntegerDefault("minSdkVersion")
    targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")

    externalNativeBuild {
      cmake {
        cppFlags "-frtti -fexceptions -Wall -Wextra -fstack-protector-all"
        arguments "-DANDROID_STL=c++_shared",
                  "-DANDROID_TOOLCHAIN=clang",
                  "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
        abiFilters (*reactNativeArchitectures())

        buildTypes {
          debug {
            cppFlags "-O1 -g"
          }
          release {
            cppFlags "-O2"
          }
        }
      }
    }
  }

  externalNativeBuild {
    cmake {
      path "CMakeLists.txt"
    }
  }

  buildFeatures {
    buildConfig true
    prefab true
    prefabPublishing true
  }

  packagingOptions {
    pickFirst "**/libc++_shared.so"
    excludes = [
      "META-INF",
      "META-INF/**"
    ]
  }
  // ...
}

My CMake:

project(NitroModules)
cmake_minimum_required(VERSION 3.9.0)
set (CMAKE_CXX_STANDARD 20)

add_library(NitroModules SHARED MyFile.cpp)

# then I link a bunch of libraries that I need, like fbjni, android, ReactAndroid, ...

I am in a React Native environment which uses SoLoader. The library is then loaded and called from Java:

System.loadLibrary("NitroModules");

The full repo can be found at mrousavy/nitro, which does not include the test code above but shows the full build structure.

Upvotes: 0

Views: 105

Answers (0)

Related Questions