473,327 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

A global catch-all for exceptions in Windows Forms applications

Hi everyone,

Is there anyway to create some sort of catch-all in windows forms applications
that could ensure that no unexpected exceptions bring down an application?

For example, perhaps there is an event that you can watch for whenever an
exception is thrown? If this isnt possible can anyone suggest the best way
to handle unforeseen exceptions? I'm assuming that surrounding huge chunks
of application code in try catch blocks would be a programming faux pas :-)

Thanks to anyone who can offer advice

Kindest Regards

tce

Aug 5 '05 #1
2 3548
Please see the Application.ThreadException event:
http://msdn.microsoft.com/library/en...ptiontopic.asp
Aug 5 '05 #2
tce,
In addition to the other comments:

Depending on the type of application you are creating, .NET has three
different global exception handlers.

For ASP.NET look at:
System.Web.HttpApplication.Error event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomain.UnhandledException event
Use AddHandler in your Sub Main.

For Windows Forms look at:
System.Windows.Forms.Application.ThreadException event
Use AddHandler in your Sub Main.

It can be beneficial to combine the above global handlers in your app, as
well as wrap your Sub Main in a try catch itself.

There is an article in the June 2004 MSDN Magazine that shows how to
implement the global exception handling in .NET that explains why & when you
use multiple of the above handlers...

http://msdn.microsoft.com/msdnmag/is...T/default.aspx

For example: In my Windows Forms apps I would have a handler attached to the
Application.ThreadException event, plus a Try/Catch in my Main. The
Try/Catch in Main only catches exceptions if the constructor of the MainForm
raises an exception, the Application.ThreadException handler will catch all
uncaught exceptions from any form/control event handlers.

Public Shared Sub Main()
AddHandler Application.ThreadException, AddressOf
Application_ThreadException
Application.EnableVisualStyles()
Application.DoEvents()
Try
Application.Run(New MainForm)
Catch ex As Exception
' log ex for later diagnosis
' optionally show "pretty" version to user
End Try
End Sub

Private Shared Sub Application_ThreadException(ByVal sender As Object,
ByVal e As System.Threading.ThreadExceptionEventArgs)

' Log the e.Exception for later diagnosis.
' optionally show "pretty" version to user

End Sub

Hope this helps
Jay

"thechaosengine" <na> wrote in message
news:40*********************@news.microsoft.com...
| Hi everyone,
|
| Is there anyway to create some sort of catch-all in windows forms
applications
| that could ensure that no unexpected exceptions bring down an application?
|
| For example, perhaps there is an event that you can watch for whenever an
| exception is thrown? If this isnt possible can anyone suggest the best way
| to handle unforeseen exceptions? I'm assuming that surrounding huge chunks
| of application code in try catch blocks would be a programming faux pas
:-)
|
| Thanks to anyone who can offer advice
|
| Kindest Regards
|
| tce
|
Aug 5 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: adnanx82 | last post by:
Hi, Does anyone know how a script can detect whether exception handling using try/catch blocks is supported in the browser, so that it can use them to perform better error handling, and a syntax...
1
by: Steve | last post by:
I am trying to call a file delete function from inside Session_End in Global.asax.cs. However, everytime I step into my delete function from Session_End it jumps to the catch statement and I get...
10
by: A.M | last post by:
Hi, Can Global object have non-static methods? If answer is yes, then How can I access them in pages? I have following property in Global.asax.cs, but when I try to use it in pages, I receive...
2
by: JP | last post by:
I'm wondering if it's a good idea (or not) to include try-catch blocks in the Global.asax handlers (specifically, Application_Start and Session_Start). So, does anyone know if I should include...
9
by: Claudio Di Flumeri | last post by:
Hello all, I've added a global exception handler to my application in this way: Sub Main() AddHandler Application.ThreadException, AddressOf ThreadException AddHandler...
34
by: Bob | last post by:
Hi, The compiler gives Warning 96 Variable 'cmdSource' is used before it has been assigned a value. A null reference exception could result at runtime. Dim cmdSource as SQlClient.SQLDataReader...
3
by: Shuaib | last post by:
Hey! I am getting this exception. xml.parsers.expat.ExpatError But I am not able to catch it with "except xml.parsers.expat.ExpatError:" It says "NameError: global name 'xml' is not...
1
by: Groove | last post by:
Hey guys - I'm sure this is a commonly asked question but here goes. I'm trying to catch the error in my global.asax file and redirect to a error page which will email me the actual error upon...
3
by: David | last post by:
Hi, I have seen in some site implementations code has been entered into global.asax.cs in its own function, not any of the regular application or session functions that exist in here. What I...
2
by: arun1985 | last post by:
In the project i am using i am having the following code and when i upload it to the server.Its givig me the following error in the global.cs file. Server Error in '/' Application. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.