How do I catch an exception in a form  
Author Message

PostPosted: .NET Base Class Library, How do I catch an exception in a form Top

How do I create an exception handler at the form level without writing a try-catch round every event

Suppose I have a series of user controls, all of which use a limited set of known exceptions. Let's call them CloseAppException, CloseMdiException, CloseWindowException, SignalWindowException. I wish to handle these in a default manner in a Form superclass that has no controls on it.

How do I catch the exception if I have nothing to wrap a 'try' round

.NET Development14  

PostPosted: .NET Base Class Library, How do I catch an exception in a form Top

Are you suggesting that your controls will be throwing the exception, or just passing them on or just ignoring them

To take a shot in a different direction, the assumption will be that the controls need actions to be handled that they will initiate. Instead of throwing an exception, create a delegate that will be handled/consumed on the superform as you call it. If it involves screen updating, use the controls invoke to invoke the forms specific action so that it runs on the main thread to do the gui updating.


PostPosted: .NET Base Class Library, How do I catch an exception in a form Top

Thank you for your answer. I meant that the controls would throw the exceptions and they would be handled by the window. At this time we are in the normal message loop. So the question is really in two parts.

1) Can I catch these exceptions on the form

2) Am I really using an exception as a signalling device, and in .Net you should approach it diffferently

What I am really trying to achieve is to send a event / dynamic call to a parent form and or control, not caring if the controls knows what it is. For example MyWonderfulButton class may send a 'signal' to every control between itself up to and including the form that is has been enabled. MyWonderfulButton neither knows nor cares what action they take. This was quite normal in my previous environment (Powerbuilder) where these signals took the form of custom events. If any control didn't handle it then there is no problem. The event also trickled through all the inherited layers of any intermediate control.


Each language / environment seems to have its own words..

Classes: Descendant, Ancester, Parent , Derived , superclass, subclass, inherited class,

Events: Handle, Consume, Trigger, Post, Send, Process

I must get the right vernacular for the Netis



PostPosted: .NET Base Class Library, How do I catch an exception in a form Top

hmmm to my knowledge if you throw an exception the only way to catch it without throwing a runtime error would be using the try catch block - that is the whole point on handling exceptions.

You should be looking at using events/delegates to send "signals" to its members/subscribers so the class(es) can catch them and do whatever you want them to do.


if you are not familiar with events/delegates: df=100&forumid=16519&exp=0&select=584384 url=/library/en-us/cpguide/html/cpconeventsdelegates.asp

Hope it helps


Also for your information, throwing exceptions (well, depends what type of exceptions) are expensive, so be sure to throw the appropriate exception than a general Exception.

As I read more and more into your response, I think you need to use events and delegates to do what you are wanting to do. This is the proper way of doing things.


PostPosted: .NET Base Class Library, How do I catch an exception in a form Top


Open your your project properties and check the "Enable Application Framework" checkbox located on the application tab. Then click the "View Application Events" button.

Code Snippet

Partial Friend Class MyApplication

'Add the following sub

Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException

'Add your handling code here.

End Sub

End Class

In C#

Code Snippet

static class Program


/// <summary>

/// The main entry point for the application.

/// </summary>


static void Main()




Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

Application.Run(new Form1());


static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)




Any unhandled exceptions will call this event handler.