Override default exception dialog

D

Dacris Software

Just wondering, is there a way to disable or override the default .NET
exception dialog with your own for an entire application?
 
H

Herfried K. Wagner

Hello,

Dacris Software said:
Just wondering, is there a way to disable or override the default .NET
exception dialog with your own for an entire application?

Try this:

\\\
Public Class Main
Public Sub Main()
Try
Application.Run(New MainForm())
Catch ex As Exception
...
End Try
End Sub
End Class
///

Regards,
Herfried K. Wagner
 
J

Jay B. Harlow [MVP - Outlook]

Dacris,
Wrapping your Application.Run method with a Try/Catch block will not catch
unhandled exceptions.

The events occur asynchronously, in response to mouse clicks & other user or
system actions, if you were to catch these after Application.Run your app
would have stopped running (Application.Run is a 'Message Pump' loop, you
do not want that loop to exit, unless you are exiting your app).

If you want to avoid the window that says there was an unhandled exception,
you can use global exception handlers in your .NET app.

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.

Something like:

Public Sub Main()
AddHandler Application.ThreadException, AddressOf OnThreadException
Application.Run(New MainForm)
End Sub

Private Sub OnThreadException(sender As Object, _
ByVal e As System.Threading.ThreadExceptionEventArgs)
Try
' Show your own form
Catch ex As Exception
' Don't want to end the app here! ;-)
End Try
End Sub

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top