Exceptions, not being caught outside of the IDE!!!

  • Thread starter Thread starter Rigga
  • Start date Start date
R

Rigga

Hi all,

I am trying to catch the exceptions from a VB.NET app.

I use a startup project, so that i can show the MDI main application window
as a dialog within a Try Catch block. se below..

Try

Dim f as MainForm
f.ShowDialog()

Catch ex as Exception
' write nice error reporting routine!
End Try

Then in the Catch ex as Exception.. I write a nice error file..

This all works fine when i run the App from within the IDE, but when i run
outside of the IDE i.e. run the .exe file. it doesn't seem to catch the
exception???

It brings up a dialog box, with the error details, and allows the user to
hit the continue button and carry on as if nothing happened!?

Anyone else had this problem? or am i just doing something wrong?

Thanks in Advance.
R.
 
* "Rigga said:
I use a startup project, so that i can show the MDI main application window
as a dialog within a Try Catch block. se below..

Try

Dim f as MainForm
f.ShowDialog()

Catch ex as Exception
' write nice error reporting routine!
End Try

Then in the Catch ex as Exception.. I write a nice error file..

This all works fine when i run the App from within the IDE, but when i run
outside of the IDE i.e. run the .exe file. it doesn't seem to catch the
exception???

It brings up a dialog box, with the error details, and allows the user to
hit the continue button and carry on as if nothing happened!?

Add a handler to 'Application.ThreadException'...
 
Hi Rigga,

In what situation are you able to get an exeption in this routine?
Try
Dim f as MainForm
f.ShowDialog()
Catch ex as Exception
' write nice error reporting routine!
End Try

Or is mainform the class itself and is it a kind of endless loop?

Cor
 
Hi Cor,

The mainform, is an MDI form that controls the application.

It uses showdialog for the purposes of catching the exceptions from anywhere
within the MDI form, or other forms contained within it. the actually app
contains more than 100 different forms. all running within the MDI. if
selected of course!

And as Herfried has provided a solution to the exception catching, it all
works perfectly.

R.
 
Thanks David.

David Williams said:
Assuming that the code that you showed is in a module, and the subroutine
is the startup routine, then you need to add handlers for two different
events in this module, Threading.ThreadAbortException and
System.UnhandledException.
ThreadAbortExceptionHandler will handle non-controlled shutdown of the
application while the UnhandledExceptionHandler will catch all other
exceptions that caused the application to crash.
I normally just tell the user and log (to the EventLog) the
ThreadAbortException while in the UnhandledExceptionHandler gets the
full-blown exception reporting handling.
 
Rigga,
You should not use ShowDialog to show your MDI form, use Application.Run
along with the 'Application.ThreadException' that Herfried & I pointed out.
The article I mention in my other post also includes the events that David
is referring to.

Hope this helps
Jay
 
Rigga,
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/issues/04/06/NET/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
 
Hi Jay,

Thanks for both your posts, I think a change to the way the App start's is
required.

Again, thanks for your input. Much appreciated.

R.
 
Back
Top