Android Guides | Samples

Android.Runtime.AndroidEnvironment.UnhandledExceptionRaiser Event

Occurs when translating a managed exception into an Android throwable.

Syntax

public static event EventHandler<RaiseThrowableEventArgs> UnhandledExceptionRaiser

Remarks

When Android calls into managed code and the managed code throws an Exception, the exception needs to be caught at the Android/managed boundary and handled appropriately. For example, the exception could be ignored entirely, or the exception could be translated into a Throwable instance and raised within Android by using AndroidEnvironment.RaiseThrowable(Throwable).

If an event handler chooses to handle this event, it should set the RaiseThrowableEventArgs.Handled property to true.

Note: You cannot rely on the AppDomain.UnhandledException event as managed exceptions are never unhandled in MonoDroid; they are always intercepted at the Android/managed boundary within a catch(Exception) block.
Operation
The UnhandledExceptionRaiser event is raised when an unhandled exception is caught at the Android/managed boundary. All registered event handlers will be invoked in order of registration until one of them sets RaiseThrowableEventArgs.Handled to true. If there are no registered handlers or none of them set RaiseThrowableEventArgs.Handled to true, then Throwable.FromException(Exception) is used to translate the exception instance into a closest-matching Throwable instance, and then AndroidEnvironment.RaiseThrowable(Throwable) is invoked.
Usage
Subscribers to the UnhandledExceptionRaiser event must ensure that AndroidEnvironment.RaiseThrowable(Throwable) is not invoked more than once as part of a managed-to-Android transition, and should thus set RaiseThrowableEventArgs.Handled to true if they do so.

Requirements

Namespace: Android.Runtime
Assembly: Mono.Android (in Mono.Android.dll)
Assembly Versions: 0.0.0.0