Forum Xamarin.Android

Application ExcepionHandler

alberto.palberto.p ITMember ✭✭

I tryed to implement IUncaughtExceptionHandler

public class DefaultExceptionHandler : Java.Lang.Object, Java.Lang.Thread.IUncaughtExceptionHandler


    public void UncaughtException( Java.Lang.Thread t,  Java.Lang.Throwable e) 




I set in activity:

Java.Lang.Thread.DefaultUncaughtExceptionHandler = new DefaultExceptionHandler ();

but when I get an exception UncaughtException is not called


  • rmaciasrmacias USBeta, University ✭✭✭✭✭

    My guess is that this is designed to catch exceptions thrown by the Dalvik runtime. It will probably not catch exceptions thrown by the mono runtime (i.e. C# exceptions). That's my guess. Try throwing a Java exception and see if it catches it.

  • alberto.palberto.p ITMember ✭✭

    @rmacias In fact, you're right.
    If I use null object or division by zero, I get exception, but DefaultUncaughtExceptionHandler is not called.

    Is there a way to centralize all exceptions?
    In .Net application I use:
    Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

  • rmaciasrmacias USBeta, University ✭✭✭✭✭

    Also, one thing to note about Bradley's solution is that when the unhandled exception handler has fired, the application has already crashed. So you will not be able to gracefully recover from the exception. At this point, the only thing you can do is Log the error. What I've done before was to write the file to disk, and then the next time the app start up, I submit the error to an error logging service.

  • alberto.palberto.p ITMember ✭✭

    I found this example

    I tried to replicated in my application, but when I get exception the app crashed and don't restart.

  • rmaciasrmacias USBeta, University ✭✭✭✭✭
    edited May 2013

    Yes, I think that's the way it works. Whether you use AndroidEnvironment.UnhandledExceptionRaiser or AppDomain.CurrentDomain.UnhandledException. By the time these event handlers get fired, the application has already crashed and is not recoverable. At this point in time, Android has already killed your application. The only thing you can do is log the exception that caused the crash. If you read the comments in the example, it explains this.

Sign In or Register to comment.