473,545 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3563
Please see the Application.Thr eadException 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.Http Application.Err or event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomai n.UnhandledExce ption event
Use AddHandler in your Sub Main.

For Windows Forms look at:
System.Windows. Forms.Applicati on.ThreadExcept ion 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.Thr eadException 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.Thr eadException handler will catch all
uncaught exceptions from any form/control event handlers.

Public Shared Sub Main()
AddHandler Application.Thr eadException, AddressOf
Application_Thr eadException
Application.Ena bleVisualStyles ()
Application.DoE vents()
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_Thr eadException(By Val sender As Object,
ByVal e As System.Threadin g.ThreadExcepti onEventArgs)

' 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******** *************@n ews.microsoft.c om...
| 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
3214
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 error is not produced in cases where they are not supported? I know that IE and Netscape 5 and above support exception handling (and older...
1
4937
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 the "Object reference not set to an instance of an object." I don't understand why it is happening. I would appreciate any help, thanks. ...
10
2335
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 error "An object reference is required for the nonstatic field, method, or property CurrentDisplayName":
2
1900
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 try-catch blocks in my Global.asax handlers? And, if so, what should I do with the caught exception (i.e., what do I do in the catch block)? ...
9
2200
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 AppDomain.CurrentDomain.UnhandledException, AddressOf UnhandledException Application.Run(Main)
34
4847
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 Try Set up the database read and do it. Catch ex as system.exception exception stuff here Finally
3
5550
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 defined".
1
1899
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 page load. Everything seems to work OK except catch the actual error. It detects the error when I break the connection string to MS SQL or pause the...
3
2626
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 want to be able to do is to have workflow in my project. The workflow may be extended by future programmers by adding code into the global.asax.cs...
2
2323
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. -------------------------------------------------------------------------------- Object reference not set to an instance of an object. Description: An...
0
7478
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7410
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7923
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5984
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3466
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.