469,903 Members | 1,503 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

Catching every unhandled exception?

Elp
Hi,

I've developped a Window Form C# application which main form contains
several activex controls. No problems most of the time but on some machine,
the application crashes when the main form is closed with a
NullReferenceException caused by the DisposeAxControl which, i guess, is
supposed to dispose one of my ActiveX.

This is not a big problem as the application is closing anyway but i'd like
to suppress this crash message. However, i can't find a way to catch this
exception in my application. Even when putting the whole main method in a
try catch block, this exception is not catched! How can i catch all
unhandeled exceptions in an application?
Nov 16 '05 #1
3 16999
Hi Elp,
To catch Windows Form's unhandled exception hook-up
AppDomain.UnhandledException and Application.ThreadException events

--
HTH
Stoitcho Goutsev (100) [C# MVP]
"Elp" <ro********@REMOVEME.hotmail.com> wrote in message
news:O8**************@TK2MSFTNGP09.phx.gbl...
Hi,

I've developped a Window Form C# application which main form contains
several activex controls. No problems most of the time but on some machine, the application crashes when the main form is closed with a
NullReferenceException caused by the DisposeAxControl which, i guess, is
supposed to dispose one of my ActiveX.

This is not a big problem as the application is closing anyway but i'd like to suppress this crash message. However, i can't find a way to catch this
exception in my application. Even when putting the whole main method in a
try catch block, this exception is not catched! How can i catch all
unhandeled exceptions in an application?

Nov 16 '05 #2
Elp,
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.

Hope this helps
Jay
"Elp" <ro********@REMOVEME.hotmail.com> wrote in message
news:O8**************@TK2MSFTNGP09.phx.gbl...
Hi,

I've developped a Window Form C# application which main form contains
several activex controls. No problems most of the time but on some machine, the application crashes when the main form is closed with a
NullReferenceException caused by the DisposeAxControl which, i guess, is
supposed to dispose one of my ActiveX.

This is not a big problem as the application is closing anyway but i'd like to suppress this crash message. However, i can't find a way to catch this
exception in my application. Even when putting the whole main method in a
try catch block, this exception is not catched! How can i catch all
unhandeled exceptions in an application?

Nov 16 '05 #3
Elp

"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:u7**************@TK2MSFTNGP12.phx.gbl...
Hi Elp,
To catch Windows Form's unhandled exception hook-up
AppDomain.UnhandledException and Application.ThreadException events


Thanks to both of you. I think that it is what i was looking for. I'll give
it a try.
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Professor Frink | last post: by
5 posts views Thread by Lucvdv | last post: by
reply views Thread by Gene Hubert | last post: by
1 post views Thread by Parag Mahajan | last post: by
reply views Thread by Mike S | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.