Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm writing some code to better deliver error messages to users, I was wondering if anyone might have some source that they think might work well for delivering information from exceptions from try and catch. I've been using the console to catch the errors in development, but this isn't very useful to the user.

 

If anyone could help it'd be cool. If not, I'll survive.

Posted

If you have a windows forms, you can deliver the message through a messagebox for example.

MessageBox.Show(ex.Message)

 

or use the ErrorProvider Control (you can search for help on MSDN for the error provider)

 

 

Hope this helps,

Dream as if you'll live forever, live as if you'll die today
Posted

Heres a snippet of code that traps any errors running on the application thread. It does let some by (sub threads of other threads) but gets most.

 


 Dim eh As CustomExceptionHandler = New CustomExceptionHandler()
' Adds the event handler to the event.
 AddHandler Application.ThreadException, AddressOf eh.OnThreadException


' Creates a class to handle the exception event.
   Private Class CustomExceptionHandler

       ' Handles the exception event.
       Public Sub OnThreadException(ByVal sender As Object, ByVal t As System.Threading.ThreadExceptionEventArgs)
           Dim result As DialogResult = System.Windows.Forms.DialogResult.Cancel
           Try
               result = Me.ShowThreadExceptionDialog(t.Exception)
           Catch
               Try
                   MessageBox.Show("Fatal Error", "Fatal Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop)
               Finally
                   Application.Exit()
               End Try
           End Try

           ' Exits the program when the user clicks Abort.
           If (result = System.Windows.Forms.DialogResult.Abort) Then
               Application.Exit()
           End If
       End Sub

       ' Creates the error message and displays it.
       Private Function ShowThreadExceptionDialog(ByVal e As Exception) As DialogResult

           Dim errorMsg As String = "An error occurred please contact the administrator with the following information:" & vbCrLf & vbCrLf
           errorMsg &= e.Message & vbCrLf
           errorMsg &= "Stack Trace:" & vbCrLf
           errorMsg &= e.StackTrace & vbCrLf

           Return MessageBox.Show(errorMsg, "Application Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop)

       End Function
   End Class

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...