Trevor
Trevor

Reputation: 2206

Unhandled Exception Handling

In my MonoDroid application, when an unhandled exception occurs the application terminates without any messages. If I can reproduce the error on my local device I can debug it through Visual Studio without any problems.

However, on remote devices I am stuck for a solution.

I have tried the following in the Application class but it does not actually write my log file, unless I am running through the debugger in Visual Studio.

public override void OnCreate()
    {
        base.OnCreate();                                    
        AndroidEnvironment.UnhandledExceptionRaiser += new EventHandler<RaiseThrowableEventArgs>(AndroidEnvironment_UnhandledExceptionRaiser);
    }

    void AndroidEnvironment_UnhandledExceptionRaiser(object sender, RaiseThrowableEventArgs e)
    {
        // Write Log File
    }

Upvotes: 0

Views: 1732

Answers (2)

Jim G.
Jim G.

Reputation: 15363

I disagree with @SpiritMachine's answer.

Mono documentation tells us:

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.

Instead, I recommend that you do the following:

AndroidEnvironment.UnhandledExceptionRaiser += (sender, args) =>
{
    // Do something...
};

Upvotes: 3

manadart
manadart

Reputation: 1810

Try something like this:

EDIT : This code cannot handle caught errors. Please see @Jim G.'s answer....

Personally, I would localise error handling to where you need it. The reason being, you don't know what your application state will be when this handler is recruited - you may be without resources that you're depending on to do the handling...

Upvotes: -1

Related Questions