473,398 Members | 2,212 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,398 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 5714
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: 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
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.