SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software Kodyaz Development Resources
Development resources, articles, tutorials, samples, codes and tools for .Net, SQL Server, Vista, etc.

Tuba



Handling Unhandled Exceptions in Windows Forms

I myself don't enjoy reading long articles, complex explanations and less code. For those who do enjoy, you can have a look at the msdn version here : http://msdn.microsoft.com/msdnmag/issues/04/06/NET/default.aspx

For the rest, here you can find what works for me:

I added the following code segment to my base form in  Public Sub New()

'The 2 event handlers

'add an unhandled exceptions handler

Dim currentDomain As AppDomain = AppDomain.CurrentDomain

'for regular unhandled stuff

AddHandler currentDomain.UnhandledException, AddressOf MYExceptionHandler

'for threads behind forms

AddHandler Application.ThreadException, AddressOf MYThreadHandler

I added the MYThreadHandler and MYExceptionHandler functions to the same form as well.

Private Sub MYExceptionHandler(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)

Dim EX As Exception

EX = CType(e.ExceptionObject, Exception)

MessageBox.Show(EX.StackTrace)

End Sub

Private Sub MYThreadHandler(ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs)

MessageBox.Show(e.Exception.StackTrace)

End Sub

For all the other forms inherited from my base class, any exception in the code without a try and catch block go directly to my handling section.

You also should pay attention to configure your applications Exception Settings under the Debug menu to Continue when the exception is not handled. So that the big window asking you to Break or Continue when an exception occurs will not appear and the code will keep on without being interrupted.

In our project we send mail messages to the related developers after catching exceptions. The mail process is using publisher. I will post the code soon. Enjoy coding..

Published Thursday, July 06, 2006 11:40 AM

Comments

No Comments
Anonymous comments are disabled
This Blog



Add to Google

Syndication
Copyright © 2004 - 2014 Eralper Yilmaz. All rights reserved.
Community Server by Telligent Systems