user853710
user853710

Reputation: 1767

Catching debugging error message in one single place

I am working on a big application that has a lot of levels of hierarchy, and I am getting to a point where I am going nuts about calling logging messages and forgetting about them. From time to time while testing I run into exceptions. Now I am forwarding error messages to my main form through interfaces and other ways.

Is there a way to make the application in a case of an exception to always trigger a single method in the main form.

I mean, now I catch the exception and forward it through the interfaces. I would like to be able to ignore this and every time an exception happens, execute automaticly one method in the main form

Upvotes: 0

Views: 80

Answers (1)

p.s.w.g
p.s.w.g

Reputation: 148980

If you allow the exceptions to bubble up through the application you can use AppDomain.UnhandledException. You should allow any exception which you cannot meaningfully handle to bubble up anyway.

Something like this should do the trick:

class Program
{
    public static void Main()
    {
        form = new MainForm();
        AppDomain.CurrentDomain.UnhandledException +=
            new UnhandledExceptionEventHandler(form.OnUnhandledException);
        Application.Run(form);
    }
}

Upvotes: 1

Related Questions