Global Exception Handler

G

Guest

I am working on a VB.NET application and instead of throwing Try Catch blocks
all over the place, I have a custom applcation context which has a exception
handler..

Public Class ContextManager
Inherits ApplicationContext

Public Sub New
Dim handler As ThreadExceptionHandler = New ThreadExceptionHandler
AddHandler Application.ThreadException, AddressOf
handler.Application_ThreadException

Dim frm as new frmMain
frm.show()

.... Rest of SUB omitted ...
End Sub

Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub

End Class

Friend Class ThreadExceptionHandler

Public Sub Application_ThreadException( _
ByVal sender As System.Object, _
ByVal e As ThreadExceptionEventArgs)

Try
'Exit the program if something other than OK is sent.
If ShowThreadExceptionDialog(e.Exception) Then
If TypeOf e.Exception Is
BusinessLogicLayer.Security.SecurityException Then End
End If
Catch
Try 'Fatal error, terminate program
MessageBox.Show("Sales Manager",
BusinessLogicLayer.StringResource.CriticalSystemError, _
MessageBoxButtons.OK, MessageBoxIcon.Stop)
Finally
End
End Try
End Try
End Sub

Private Function ShowThreadExceptionDialog(ByVal ex As Exception) As
DialogResult

'Dim errorMessage As String = _
'"An error has occured in this application." _
'& vbCrLf & vbCrLf & ex.Message.ToString & vbCrLf & vbCrLf &
ex.GetType().ToString()

Dim errorMessage As String
If TypeOf ex Is BusinessLogicLayer.Security.SecurityException Then
errorMessage = "A security access violation has been detected."
& vbCrLf & vbCrLf & _
ex.Message.ToString & vbCrLf & vbCrLf & "In order to protect
data from possible corruption, this application will exit."
Else
errorMessage = "An error has been detected in this application."
& vbCrLf & vbCrLf & _
ex.Message.ToString
End If

Return MessageBox.Show(errorMessage, "Sales Manager",
MessageBoxButtons.OK, MessageBoxIcon.Stop)
End Function

End Class ' ThreadExceptionHandler

This works great in my application. If an exception in thrown in Main or
anything I call with .ShowDialog it will handle. Now, if I launch a form
such as frm.Show() from my Main Form exceptions are unhandled and the app
crashes. Is there an easy way to get around this? I cannot use .ShowDialog
everywhere as it would make the app difficult to use. Some forms must be
able to be shown more than once hence the .Show() My application checks for
certain security permissions before invoking a class, which I have a custom
exception for this BusinessLogicLayer.Security.SecurityException which logs
the occurance. In my handler I have it close the app if this occurs. Other
exceptions I have set to just show a messagebox. I am not sure as mentioned
before how to make this work for frm.Show()

Thanks in advance...
 
S

Steven Cheng[MSFT]

Hi,

Welcome to MSDN newsgroup.
Regarding on the problem with using ThreadException handler in winform app
you mentioned, here are some of my udnerstanding and suggestions:

1. The Application.ThreadException event handler is used to capture those
unhandled exceptions occurs on the application's main UI THREAD. And
genearlly all the forms displaying in the winform app are running in the
same UI thread ( no matter open through form.show or showDialog).

2. If the code is executing on other workerthread (including thread pool
threads), the exception occur on those threads will not be captured by the
UI thread's ThreadException handler.

So as for your scenario, I think we should verify the following things
first:

1) Whether the problem is concerned with our custom exception in your
component. Have you tried throwing a standard .net buildin exception in the
form2's code to see whether the Application.ThreadException can capture it?

2) Whether the exception is thrown in code period executing on a
workerthread rather than the main UI thread. If so, the
Application.ThreadException can't capture it.

Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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