473,385 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1998
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Derek Wolf | last post by:
I am having some difficulties implementing an UnHandledException handler for the AppDomain.UnhandledException event in a windows service for unhandled exceptions that occur on a System.Timers.Timer...
9
by: Claudio Di Flumeri | last post by:
Hello all, I've added a global exception handler to my application in this way: Sub Main() AddHandler Application.ThreadException, AddressOf ThreadException AddHandler...
7
by: Adam J. Schaff | last post by:
I would like my windows service application to log all exceptions that occur, on any of its threads, to the windows event log. Is this possible? If so, can anyone give me an example of the...
3
by: Masa Ito | last post by:
I am trying to follow the great advice that seems to be said over and over by good people (like Jay Harlow) about putting a global exception handler in Sub Main. Our app has Sub Main in a...
1
by: Samuel Shulman | last post by:
Hi Is there a global exception handler for ASP.NET application? and can that be/is implemented? Thank you, Samuel
3
by: Simon Harvey | last post by:
Hi all, What is the best way to catch any and all exceptions when creating a windows service application? Is there some sort of global exception event I can use, or is it possible to surround...
0
by: PyNoob | last post by:
By default, uncaught exceptions stop the execution of python scripts (similar to what bash does when -e us set.) I need to change this behavior, so that the user gets informed about the uncaught...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.