473,326 Members | 2,173 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,326 software developers and data experts.

Unhandled exception was handled??

Can someone explain why this code pops up a messagebox saying the
ThreadAbortException wasn't handled?

The first exception is reported only in the debug pane, as expected.
The second (caused by thread.Abort()) is reported twice: once in the debug
window, and once through the message box.

Is it because the thread was sleeping when the exception occurred?
Imports System.Threading

[...]

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

Dim currentDomain As AppDomain = AppDomain.CurrentDomain

AddHandler currentDomain.UnhandledException, _
AddressOf UnhandledExceptionHandler

Try
Throw New Exception("This is a test")
Catch ex As Exception
Debug.WriteLine(ex.ToString)
End Try

Dim t As New Thread(AddressOf ThreadProc)
t.Start()
Thread.Sleep(1000)
t.Abort()
End Sub

Private Sub UnhandledExceptionHandler(ByVal sender As Object, _
ByVal args As UnhandledExceptionEventArgs)

MsgBox("Unhandled " & args.ExceptionObject.GetType.ToString())
End Sub

Private Sub ThreadProc()
Try
Do
Thread.Sleep(100)
Loop
Catch ex As ThreadAbortException
Debug.WriteLine(ex.ToString)
End Try
End Sub

Nov 21 '05 #1
5 5711
Lucvdv,
Windows Forms itself is displaying the Message Box, as the Load Event had an
unhandled exception.

Generally you need to handle both the AppDomain.UnhandledException & the
Application.ThreadException.

The Application.ThreadException will take care of unhandled exception in
your Windows Forms, while the AppDomain.UnhandledException will take care of
unhandled exceptions in your other threads. I would add handlers for both
exceptions first thing in my Sub Main, not the Load event, as the exceptions
could occur before the Load event occured.

Depending on the type of application you are creating, .NET has three
different global exception handlers.

For ASP.NET look at:
System.Web.HttpApplication.Error event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomain.UnhandledException event
Use AddHandler in your Sub Main.

For Windows Forms look at:
System.Windows.Forms.Application.ThreadException event
Use AddHandler in your Sub Main.

It can be beneficial to combine the above global handlers in your app, as
well as wrap your Sub Main in a try catch itself.

There is an article in the June 2004 MSDN Magazine that shows how to
implement the global exception handling in .NET that explains why & when you
use multiple of the above handlers...

http://msdn.microsoft.com/msdnmag/is...T/default.aspx

For example: In my Windows Forms apps I would have a handler attached to the
Application.ThreadException event, plus a Try/Catch in my Main. The
Try/Catch in Main only catches exceptions if the constructor of the MainForm
raises an exception, the Application.ThreadException handler will catch all

uncaught exceptions from any form/control event handlers.

Note David has some excellent comments on argument validation to your class
libraries. Especially if those class libraries are going to be used outside
of your current solution.

Hope this helps
Jay
"Lucvdv" <re**********@null.net> wrote in message
news:nl********************************@4ax.com...
Can someone explain why this code pops up a messagebox saying the
ThreadAbortException wasn't handled?

The first exception is reported only in the debug pane, as expected.
The second (caused by thread.Abort()) is reported twice: once in the debug
window, and once through the message box.

Is it because the thread was sleeping when the exception occurred?
Imports System.Threading

[...]

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

Dim currentDomain As AppDomain = AppDomain.CurrentDomain

AddHandler currentDomain.UnhandledException, _
AddressOf UnhandledExceptionHandler

Try
Throw New Exception("This is a test")
Catch ex As Exception
Debug.WriteLine(ex.ToString)
End Try

Dim t As New Thread(AddressOf ThreadProc)
t.Start()
Thread.Sleep(1000)
t.Abort()
End Sub

Private Sub UnhandledExceptionHandler(ByVal sender As Object, _
ByVal args As UnhandledExceptionEventArgs)

MsgBox("Unhandled " & args.ExceptionObject.GetType.ToString())
End Sub

Private Sub ThreadProc()
Try
Do
Thread.Sleep(100)
Loop
Catch ex As ThreadAbortException
Debug.WriteLine(ex.ToString)
End Try
End Sub

Nov 21 '05 #2
On Thu, 26 Aug 2004 15:19:35 -0500, "Jay B. Harlow [MVP - Outlook]"
<Ja************@msn.com> wrote:
Lucvdv,
Windows Forms itself is displaying the Message Box, as the Load Event had an
unhandled exception.


Thanks for the information, but it still isn't clear to me.

Thread.Abort() causes a ThreadAbortException in the thread it's called on,
but not in the thread where it is called from -- so there was no unhandled
exception in the Load event.
The debug output at the bottom of this message confirms this.
In the mean time I solved my problem by just adding an IF that throws out
all ThreadAbortExceptions, but I'd still like to know why it's happening.
I was expecting that only those exceptions the debugger would normally
catch or that would make an error dialog pop up in the compiled program,
would be reported through the UnhandledException event.

It seems completely illogical that the same exception would ever be handled
twice, but that's exactly what is happening here.
In my example code, when there's no unhandled exception handler, nothing is
reported by the debugger (just comment out the AddHandler line, and the
program runs without error).

If there is such a handler, the ThreadAbortException is *first* caught by
the Catch clause in the thread, and *then* still reported as unhandled (see
output below).
I created a new console application and slightly modified the previous code
so I could see where the exception is handled first, and where it's
reported to have occurred:

'-----------------
Imports System.Threading

Module Module1

Dim thr As Thread

Sub Main()
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
AddHandler currentDomain.UnhandledException, _
AddressOf UnhandledExceptionHandler

thr = New Thread(AddressOf ThreadProc)
thr.Start()
Thread.Sleep(1000)
thr.Abort()
End Sub

Private Sub UnhandledExceptionHandler(ByVal sender As Object, _
ByVal args As UnhandledExceptionEventArgs)
Dim ex As ThreadAbortException = CType(args.ExceptionObject,
ThreadAbortException)
Debug.WriteLine("Unhandled " & ex.ToString())
System.Console.WriteLine("Unhandled " & ex.ToString())
End Sub

Private Sub ThreadProc()
Try
Do
Thread.Sleep(100)
Loop
Catch ex As ThreadAbortException
Debug.WriteLine("Handled " & ex.ToString)
System.Console.WriteLine("Handled " & ex.ToString())
End Try
End Sub

End Module
'-----------------
The output, either in the debug pane or at the console in the compiled
program, shows that the exception is handled twice:

Handled System.Threading.ThreadAbortException: Thread was being aborted.
at System.Threading.Thread.Sleep(Int32 millisecondsTimeout)
at ThreadAbortExceptionTest2.Module1.ThreadProc()
in D:\My Programs\Test\ThreadAbortExceptionTest2\Module1.vb :line 28
Unhandled System.Threading.ThreadAbortException: Thread was being aborted.
at ThreadAbortExceptionTest2.Module1.ThreadProc()
in D:\My Programs\Test\ThreadAbortExceptionTest2\Module1.vb :line 32
The thread '<No Name>' (0xb08) has exited with code 0 (0x0).
The program '[2952] ThreadAbortExceptionTest2.exe' has exited with code 0
(0x0).

If you comment out line 32 (System.Console.Writeline), the 'unhandled'
message changes to line 31 (Debug.Writeline). Comment that out too, and it
changes to line 30 (Catch).
Comment out the AddHandler, and it becomes:

Handled System.Threading.ThreadAbortException: Thread was being aborted.
at System.Threading.Thread.Sleep(Int32 millisecondsTimeout)
at ThreadAbortExceptionTest2.Module1.ThreadProc()
in D:\My Programs\Test\ThreadAbortExceptionTest2\Module1.vb :line 28
The thread '<No Name>' (0xb08) has exited with code 0 (0x0).
The program '[2952] ThreadAbortExceptionTest2.exe' has exited with code 0
(0x0).

without any unhandled exception.

Nov 21 '05 #3
ThreadAbortException is a special exception that can be caught, but it will
automatically be raised again at the end of the catch block, and re-thrown
it. This is propagated all the way up, so that everyone in the chain gets a
chance to do any cleanup that may be necessary, so that the thread
terminates gracefully.

Luke

Nov 21 '05 #4
Lucvdv,
Doh! I read you sample code too quickly rather then try it.
As you pointed out Thread.Abort raises ThreadAbortException, as Luke pointed
out ThreadAbortException is automatically raised again.
To prevent ThreadAbortException from being automatically raised again you
need to use Thread.ResetAbort.
Hope this helps
Jay

"Lucvdv" <re**********@null.net> wrote in message
news:v6********************************@4ax.com...
On Thu, 26 Aug 2004 15:19:35 -0500, "Jay B. Harlow [MVP - Outlook]"
<Ja************@msn.com> wrote:
Lucvdv,
Windows Forms itself is displaying the Message Box, as the Load Event had an unhandled exception.
Thanks for the information, but it still isn't clear to me.

Thread.Abort() causes a ThreadAbortException in the thread it's called on,
but not in the thread where it is called from -- so there was no unhandled
exception in the Load event.
The debug output at the bottom of this message confirms this.
In the mean time I solved my problem by just adding an IF that throws out
all ThreadAbortExceptions, but I'd still like to know why it's happening.
I was expecting that only those exceptions the debugger would normally
catch or that would make an error dialog pop up in the compiled program,
would be reported through the UnhandledException event.

It seems completely illogical that the same exception would ever be

handled twice, but that's exactly what is happening here.
In my example code, when there's no unhandled exception handler, nothing is reported by the debugger (just comment out the AddHandler line, and the
program runs without error).

If there is such a handler, the ThreadAbortException is *first* caught by
the Catch clause in the thread, and *then* still reported as unhandled (see output below).
I created a new console application and slightly modified the previous code so I could see where the exception is handled first, and where it's
reported to have occurred:

'-----------------
Imports System.Threading

Module Module1

Dim thr As Thread

Sub Main()
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
AddHandler currentDomain.UnhandledException, _
AddressOf UnhandledExceptionHandler

thr = New Thread(AddressOf ThreadProc)
thr.Start()
Thread.Sleep(1000)
thr.Abort()
End Sub

Private Sub UnhandledExceptionHandler(ByVal sender As Object, _
ByVal args As UnhandledExceptionEventArgs)
Dim ex As ThreadAbortException = CType(args.ExceptionObject,
ThreadAbortException)
Debug.WriteLine("Unhandled " & ex.ToString())
System.Console.WriteLine("Unhandled " & ex.ToString())
End Sub

Private Sub ThreadProc()
Try
Do
Thread.Sleep(100)
Loop
Catch ex As ThreadAbortException
Debug.WriteLine("Handled " & ex.ToString)
System.Console.WriteLine("Handled " & ex.ToString())
End Try
End Sub

End Module
'-----------------
The output, either in the debug pane or at the console in the compiled
program, shows that the exception is handled twice:

Handled System.Threading.ThreadAbortException: Thread was being aborted.
at System.Threading.Thread.Sleep(Int32 millisecondsTimeout)
at ThreadAbortExceptionTest2.Module1.ThreadProc()
in D:\My Programs\Test\ThreadAbortExceptionTest2\Module1.vb :line 28
Unhandled System.Threading.ThreadAbortException: Thread was being aborted.
at ThreadAbortExceptionTest2.Module1.ThreadProc()
in D:\My Programs\Test\ThreadAbortExceptionTest2\Module1.vb :line 32
The thread '<No Name>' (0xb08) has exited with code 0 (0x0).
The program '[2952] ThreadAbortExceptionTest2.exe' has exited with code 0
(0x0).

If you comment out line 32 (System.Console.Writeline), the 'unhandled'
message changes to line 31 (Debug.Writeline). Comment that out too, and it changes to line 30 (Catch).
Comment out the AddHandler, and it becomes:

Handled System.Threading.ThreadAbortException: Thread was being aborted.
at System.Threading.Thread.Sleep(Int32 millisecondsTimeout)
at ThreadAbortExceptionTest2.Module1.ThreadProc()
in D:\My Programs\Test\ThreadAbortExceptionTest2\Module1.vb :line 28
The thread '<No Name>' (0xb08) has exited with code 0 (0x0).
The program '[2952] ThreadAbortExceptionTest2.exe' has exited with code 0
(0x0).

without any unhandled exception.

Nov 21 '05 #5
On Mon, 30 Aug 2004 08:38:27 GMT, lu******@online.microsoft.com ([MSFT])
wrote:
ThreadAbortException is a special exception that can be caught, but it will
automatically be raised again at the end of the catch block, and re-thrown
it. This is propagated all the way up, so that everyone in the chain gets a
chance to do any cleanup that may be necessary, so that the thread
terminates gracefully.


Thanks (and to Jay too).

I'll sleep a bit better now that I know it's nothing abnormal ;)

Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Oliver | last post by:
Hello, I may have posted in the wrong place, if so, feel free to move my post (just notify me where you put it via email or something) Iā€™m having a problem with my program that I cant...
4
by: Frank | last post by:
Hi, I made a handler for unhandled errors. But before that is executed, VB.NET gives me the standard error window. In VB6 there was a setting (errortrapping) about handling errors in the design...
5
by: Samuel R. Neff | last post by:
When you have an unhandled exception in vb.net how do you view the exception information in the debugger? In C# the debugger creates a local variable that points to the exception and you can...
3
by: Nick | last post by:
Hi there, This probably wont make much sense but I'm getting an unhandled exception being thrown in the following line... Try While True Redraw control as necessary End While Catch ex as...
3
by: NickP | last post by:
Hi there, Im experiencing an unhandled exception that seems to be impossible for me to catch. I am launching an assembly of mine from within another assembly using the Process object. Once the...
2
by: Bob | last post by:
I MUST be able to trap unhandled exceptions, bring the thread to a routine that then closes the thread on which the execption occurred without closing or affecting the other threads. Think of an...
6
by: Viktar Zhardzetski | last post by:
Hi everybody, let me start with a simple sample to replicate the problem: 1. Create a new Windows Application C# project with 2 forms - MainForm and FaultyForm; 2. In the faulty form...
1
by: bg_ie | last post by:
Hi, I have the following Program.cs - namespace TestFrameworkApplication { static class Program { /// <summary> /// The main entry point for the application.
1
Shashi Sadasivan
by: Shashi Sadasivan | last post by:
Hi all, I have got my windows app to handled any UI or unhandled exceptions. following is the code static class Program { /// <summary> /// The main entry point for the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.