35

When working on ASP.NET 1.1 projects I always used the Global.asax to catch all errors. I'm looking for a similar way to catch all exceptions in a Windows Forms user control, which ends up being a hosted IE control. What is the proper way to go about doing something like this?

Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
Alex
  • 1,576
  • 2
  • 15
  • 28
  • Also have a look at [my question](http://stackoverflow.com/questions/944/unhandled-exception-handler-in-net-11) for some of the pitfalls (links to a couple of coding horror blog entries). – Ray Aug 05 '08 at 22:17

5 Answers5

25

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.

Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
Chris Leon
  • 411
  • 4
  • 5
11

Currently in my winforms app I have handlers for Application.ThreadException, as above, but also AppDomain.CurrentDomain.UnhandledException

Most exceptions arrive via the ThreadException handler, but the AppDomain has also caught a few in my experience

Rizwan
  • 103
  • 4
  • 24
Orion Edwards
  • 121,657
  • 64
  • 239
  • 328
  • 3
    Sample code from MSDN showing how to catch both types of unhandled exceptions: [msdn](http://msdn.microsoft.com/en-us/library/system.windows.forms.application.threadexception.aspx) – Jan Hettich Jul 14 '11 at 00:43
6

If you're using VB.NET, you can tap into the very convenient ApplicationEvents.vb. This file comes for free with a VB.NET WinForms project and contains a method for handling unhandled exceptions.

To get to this nifty file, it's "Project Properties >> Application >> Application Events"

If you're not using VB.NET, then yeah, it's handling Application.ThreadException.

Brad Tutterow
  • 7,487
  • 3
  • 33
  • 33
3

To Handle Exceptions Globally...

Windows Application

System.Windows.Forms.Application.ThreadException event

Generally Used in Main Method. Refer MSDN Thread Exception

Asp.Net

System.Web.HttpApplication.Error event

Normally Used in Global.asax file. Refer MSDN Global.asax Global Handlers

Console Application

System.AppDomain.UnhandledException event

Generally used in Main Method. Refer MSDN UnhandledException

Ajay
  • 2,022
  • 19
  • 33
2

Code from MSDN: http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2

Sub Main()
  Dim currentDomain As AppDomain = AppDomain.CurrentDomain
  AddHandler currentDomain.UnhandledException, AddressOf MyHandler

  Try 
     Throw New Exception("1")
  Catch e As Exception
     Console.WriteLine("Catch clause caught : " + e.Message)
     Console.WriteLine()
  End Try 

  Throw New Exception("2")
End Sub 

Sub MyHandler(sender As Object, args As UnhandledExceptionEventArgs)
  Dim e As Exception = DirectCast(args.ExceptionObject, Exception)
  Console.WriteLine("MyHandler caught : " + e.Message)
  Console.WriteLine("Runtime terminating: {0}", args.IsTerminating)
End Sub 
Aaron Hoffman
  • 6,604
  • 8
  • 56
  • 61