471,330 Members | 1,607 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

Global Exception Handler

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.CriticalSystemEr ror, _
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...
Jul 21 '05 #1
1 1858
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.)


Jul 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Claudio Di Flumeri | last post: by
3 posts views Thread by Masa Ito | last post: by
1 post views Thread by Samuel Shulman | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.