473,379 Members | 1,422 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,379 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 1997
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.