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, code samples and tools and downloads for ASP.Net, SQL Server, R Script, Windows, Windows Phone, AWS, SAP HANA and ABAP, like SAP UI5, Screen Personas, etc.


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)


End Sub

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


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


No Comments
Anonymous comments are disabled
This Blog
Copyright © 2004 - 2020 Eralper YILMAZ. All rights reserved.
Community Server by Telligent Systems