How to create a global error/exception handler?

In all systems I previously wrote code for I could create a global exception handler. AppDomain.CurrentDomain.UnhandledException is one example. TaskScheduler.UnobservedTaskException is another. Then, there are more specific handlers on Android or Windows Desktop. Nothing I tried worked for Tizen.NET. I, also, tried to put the whole Main in try block to no avail

  private static void Main(string[] args) {
     AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;

     try {
        var app = new Program();
        Forms.Init(app);
        FormsCircularUI.Init();
        app.Run(args);
     }
     catch(Exception objException) {
        Log.Fatal(objException.ToString());
     }
  }

Can anybody give me the list of all unhandled exception handlers to cover all exception situations ideally including exceptions coming from the very core/native?
In Debug I want to log them. Later in Release I’d like to send them to Analytics.

1 Like

Hi,
You can find detail information and implementation of AppDomain.CurrentDomain.UnhandledException in dot NET core platform here.

You can find more exception here with implementation example.

There is also a implementation issue that has been discussed in the stack overflow. You can follow that. May be it can help you to implement exception handler in your project.

It looks like you misunderstood the question. I’m perfectly aware of how to implement handling of AppDomain.CurrentDomain.UnhandledException and all other handlers I know of.

My point was that all handlers that I added to the app didn’t make a difference, including direct try/catch. Anyway, there are exceptions which are not caught. They just crash the app or make the debugger stop at app.Run(args); line if it is attached but no global exception handlers are invoked as well as the catch block.

I hoped that there is another unhandled exceptions handler somewhere in the Tizen.NET SDK that would allow me to catch unhandled exceptions. Is there?

Hi,
Sorry for the misunderstanding at first. It is really hard to understand the issue without knowing the error that you are getting from the application.

Providing error log may help to understand better.

Moreover you may know that Tizen.NET programming environment consists of the following environments.

  1. .NET Standard API
  2. Xamarin.Forms
  3. Tizen.Wearable.CircularUI
  4. TizenFX API

Check this documentation to know details about these four and their API references.

You can check API references for .NET and Xamarin.Forms in their respective site. You can also check the API reference for Tizen.Wearable.CircularUI and TizenFX API.

I’m not talking about one particular error or exception. I want to find a way to process ALL exceptions in the WHOLE app which were not handled, were not in a try/catch. I want to know if there is something similar to AppDomain.CurrentDomain.UnhandledException in Tizen.NET which would allow me to do that. As I already mentioned AppDomain.CurrentDomain.UnhandledException didn’t work. I hope there is something in the Tizen.NET API which would work.

Hello,

Checked your responses.

Looking at the ‘Applies to’ list of ‘AppDomain.CurrentDomain.UnhandledException’ and Tizen .NET Overview, it seems like ‘AppDomain.CurrentDomain.UnhandledException’ should be working on your case.

Still, this remains a matter of further analysis.

  • You can register an Issue on the Tizen C# API repo, might get some response from the contributors.
  • One thing we can suggest is to reach ‘Samsung Developer Support’ with detailed logs and code snippets. Forwarding them to the concern department might avail you some insights.

Have a good day. Regards,
Armaan Ul Islam
Samsung Developer Program

This example worked as expected on my watch device:

class Program : FormsApplication
{
    protected override void OnCreate()
    {
        base.OnCreate();

        LoadApplication(new App());

        throw new Exception("Ooops");
    }

    static void Main(string[] args)
    {
        AppDomain.CurrentDomain.UnhandledException += (s, e) =>
        {
            Tizen.Log.Fatal("MyApp", $"Caught {e.ExceptionObject}");
            Tizen.Log.Fatal("MyApp", $"Terminating!");
        };
        var app = new Program();
        Forms.Init(app);
        app.Run(args);
    }
}
F/MyApp   ( 4701): CrossTemplate9.Tizen.cs: Main(22) > Caught System.Exception: Ooops
F/MyApp   ( 4701):    at CrossTemplate9.Program.OnCreate()
F/MyApp   ( 4701):    at Tizen.Applications.CoreBackend.UICoreBackend.OnCreateNative(IntPtr data)
F/MyApp   ( 4701):    at Tizen.Applications.CoreBackend.UICoreBackend.Run(String[] args)
F/MyApp   ( 4701):    at Tizen.Applications.CoreApplication.Run(String[] args)
F/MyApp   ( 4701):    at Tizen.Applications.CoreUIApplication.Run(String[] args)
F/MyApp   ( 4701):    at CrossTemplate9.Program.Main(String[] args)
F/MyApp   ( 4701): CrossTemplate9.Tizen.cs: Main(23) > Terminating!

They just crash the app

It’s a normal behavior because handling UnhandledException events does not prevent the app from terminating.

make the debugger stop at app.Run(args)

Maybe it’s a bug in the debugger. The lldb debugger is unstable on Tizen 4.0.

I would like to target my app Tizen 4. In this case I have to use Tizen 4 emulator, right? To be sure that I test the exact behaviors on Tizen 4. This is what I did. Your example does not work on Tizen 4. It works fine on Tizen 5.5. However if I make it more complex and throw an exception somewhere from a view model, I’m using Xamarin Forms, and that view model is bound to a view the exception is swallowed somewhere in the depths of the framework or OS itself. In this case the exception does not come into the UnhandledException handler and surprisingly the app does not terminate as well. It is as if it stops responding right after that exception but it is not removed from the memory.