Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unhandled Exception handler

Status
Not open for further replies.

rosenk

Programmer
Jan 16, 2003
403
I'm trying to add a more reasonable error dialog to my application than the default for an unhandled exception. I've more or less figured out how to do this with the following code excerpts:
[tt]
Dim currentdomain As AppDomain = AppDomain.CurrentDomain
AddHandler currentdomain.UnhandledException, AddressOf CrashHandler

...

Public Shared Sub CrashHandler(ByVal sender As Object, ByVal args As UnhandledExceptionEventArgs)
Dim e As Exception = DirectCast(args.ExceptionObject, Exception)

Dim dlg As New CrashDataViewer

dlg.Exception = e
dlg.ShowDialog()
End Sub[/tt]

The [tt]CrashDataViewer[/tt] is my own dialog box that displays some information from the unhandled exception, including the stack trace and the like, giving the users the ability to copy text into the clipboard that they can paste into an email, providing crash data to the developer without the user having to understand what it means at all.

The problem is that when the user closes my dialog box, the application immediately terminates. If I leave the default exception handler, the user has the option to "Continue" and the application continues along in most cases.

How can I provide that ability in my own unhandled exception handler?

 
I had a look at the docs, and I don't see a way to not terminate. There is the IsTerminating flag on the UnhandledExceptionEventArgs parameter, but it's input only -- it's telling you whether the CLR will close, but doesn't give you the opportunity to cancel it.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Hmmm
Try this - it is not as elegant as the unhandled exception handler, but meets the needs you specify

Code:
Module Module1
    Public Sub main()
        Dim f As New Form1
Retry:
        Try
            f.ShowDialog()
        Catch ex As Exception
            GoTo Retry
        End Try
    End Sub
End Module

If you need to reset the form after the exception, then move the new form1 line inside the try

Hope this helps

Mark

Mark [openup]
 
Oh I forgot - you'll need to change the startup for the project to sub main, and of course you log or show exception details in the catch bit.


Mark [openup]
 
I guess that's better than nothing, but it would mean that, from the user perspective, the application totally restarts on them. (Though some of the more important application state would not get lost, being stored in singleton instances and such.)
 
Nope - all the state is preserved, if you choose to use the code as I posted.

I said that if you wanted to reset the forms, use the as new to do this.

All the user will see is the form flicker once, if you just log the message. If you want to show them a message, then change the code slightly and the form should still be visible while the message is on the screen.

Mark [openup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top