Winforms Global Exception Handling

WinForms Global Exception Handling?

If #26 is an exception then you can use AppDomain.CurrentDomain.UnhandledException event. If it's just a return value, then I don't see any chance to handle that globally.

public static void Main()
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);

// start main thread here
}

static void MyHandler(object sender, UnhandledExceptionEventArgs args)
{
Exception e = (Exception) args.ExceptionObject;
Console.WriteLine("MyHandler caught : " + e.Message);
}

Global Exception Handling for winforms control

You need to handle the System.Windows.Forms.Application.ThreadException event for Windows Forms. This article really helped me: http://bytes.com/forum/thread236199.html.

Global Exception Handler implementation C# WinForms

windows forms have separate event for handling exceptions on GUI thread (ie exceptions which occur on GUI events like button click, textbox text change, etc..):

Application.ThreadException += new ThreadExceptionEventHandler(Handler);

public void Handler(object sender, ThreadExceptionEventArgs t)
{
//exceptions handling code here...


// you can throw exception at the end of method if you want this exception to go unhandled, not writing throw will swallow exception
/// throw t.Exception
}

How do I create a global exception handler for winforms app that is making asyncronous WebClient calls

Any exception will be passed to your DownloadFileCompleted event handler.

This is called with a AsyncCompletedEventArgs object which has an Error property.

You just need to check this property in your handler - it won't be an "unhandled exception".

Global exception catch works only while debugging the WinForms application

You should be hooking an event such as AppDomain.UnhandledException.

These events are raised before the global error handler you're seeing in release mode. This allows you to log errors before bailing out.. in a nicer way.

There are other events that are raised also. For example, Application.ThreadException. Reading the documentation will give you better insights into your specific needs.

I have to note that the error you're seeing is a NullReferenceException.. which would ideally be nicely handled within your code. Still, hooking these events and logging exceptions is a good idea.

WinForm global catch exception for MDI child forms only

I agree that this is a bad idea in general, but if your global exception handler has a "sender" object, you could try using sender.GetType() to get the type of the object. You can see if it inherits from your base form by saying if (sender.GetType().IsSubclassOf(typeof (BaseClass))) { // then do something }.

If the sender is a child control of the child form (rather than the form itself), then you may need to walk up a few levels of ancestors using a helper method to find the parent form (eg: sender might be a textbox, sender.Parent might be a panel, sender.Parent.Parent might be the form). You can recursively check the parents until either the Parent is null, or the parent derives from your base class.

You can also loop through the MdiParent form's MdiChildren to see if any of them are the form that caused the exception. Then you wouldn't need to inherit from the base class.

Also, in the case of a ThreadException, if you have some anonymous method in a fire-and-forget type thread, this would likely not work...

I only use global exception handling in one project, and I seem to remember that it doesn't work normally in debug mode because you get the Visual Studio exception helper instead, so I don't have a quick way of testing this - just consider it a general "pseudocode" recommendation...



Related Topics



Leave a reply



Submit