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...
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...