Unmanaged library exception handling

Hi,

trying to figure out how exceptions propagate and should be handled
my test solution consists of:
1. C++Dynamic Library (.so), Target API Level android-27, exceptions enabled with "-fexceptions" flag
2. Android 9.0 (Pie) test application using library above, in OnCreate method exception handling is added as follows:

AndroidEnvironment.UnhandledExceptionRaiser += (sender, e) => ...
TaskScheduler.UnobservedTaskException += (sender, e) => ...
AppDomain.CurrentDomain.UnhandledException += (sender, e) => ...

When I throw exception in the library:

throw 0xBEEF;

Android application ends execution. No exception is caught.
What I can see in the Android Device Monitor is:

signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
Abort message: '/usr/local/google/buildbot/src/android/ndk-release-r16/external/libcxx/../../external/libcxxabi/src/abort_message.cpp:73: abort_message: assertion "terminating with uncaught exception of type int" failed'

So it seems that all exceptions should be handled in the C++ code.
In case of external libraries I can write a wrapper and handle exceptions.
Is this is a right way, or did I miss something?
If this is the right way I am looking for a documentation reference confirming it. Assuming there is any.

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    AppDomain.UnhandledException occurs when an exception is not caught. Try to detect the event with following code.

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);  
    
        AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
        TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;  
    
        Xamarin.Forms.Forms.Init(this, bundle);  
        DisplayCrashReport();  
    
        var app = new App();  
        LoadApplication(app);
    }  
    
    private static void TaskSchedulerOnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs unobservedTaskExceptionEventArgs)
    {
        var newExc = new Exception("TaskSchedulerOnUnobservedTaskException", unobservedTaskExceptionEventArgs.Exception);
        LogUnhandledException(newExc);
    }  
    
    private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
    {
        var newExc = new Exception("CurrentDomainOnUnhandledException", unhandledExceptionEventArgs.ExceptionObject as Exception);
        LogUnhandledException(newExc);
    }  
    
    
    private void DisplayCrashReport()
    {
        //
    }
    

    Refer to:
    https://stackoverflow.com/questions/41660509/catch-all-exceptions-in-android-xamarin/41661279

  • wakeupwakeup Member ✭✭

    This is exactly what I am doing.
    I have tested it on both: emulator and real hardware, result is the same.
    No exception caught.

    E/libc++abi( 5712): terminating with uncaught exception of type int
    F/libc ( 5712): Fatal signal 6 (SIGABRT), code -6 (SI_TKILL) in tid 5712 (rttest), pid 5712 (rttest)

  • wakeupwakeup Member ✭✭

    My test program can be accessed here
    https:// github.com/wakeup44/TestSample

Sign In or Register to comment.