473,854 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unhandled exception was handled??

Can someone explain why this code pops up a messagebox saying the
ThreadAbortExce ption 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.Threadin g

[...]

Private Sub Form1_Load(ByVa l sender As System.Object, _
ByVal e As System.EventArg s) Handles MyBase.Load

Dim currentDomain As AppDomain = AppDomain.Curre ntDomain

AddHandler currentDomain.U nhandledExcepti on, _
AddressOf UnhandledExcept ionHandler

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

Dim t As New Thread(AddressO f ThreadProc)
t.Start()
Thread.Sleep(10 00)
t.Abort()
End Sub

Private Sub UnhandledExcept ionHandler(ByVa l sender As Object, _
ByVal args As UnhandledExcept ionEventArgs)

MsgBox("Unhandl ed " & args.ExceptionO bject.GetType.T oString())
End Sub

Private Sub ThreadProc()
Try
Do
Thread.Sleep(10 0)
Loop
Catch ex As ThreadAbortExce ption
Debug.WriteLine (ex.ToString)
End Try
End Sub

Nov 21 '05 #1
5 5757
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.Unhan dledException & the
Application.Thr eadException.

The Application.Thr eadException will take care of unhandled exception in
your Windows Forms, while the AppDomain.Unhan dledException 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.Http Application.Err or event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomai n.UnhandledExce ption event
Use AddHandler in your Sub Main.

For Windows Forms look at:
System.Windows. Forms.Applicati on.ThreadExcept ion 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.Thr eadException 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.Thr eadException 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**********@n ull.net> wrote in message
news:nl******** *************** *********@4ax.c om...
Can someone explain why this code pops up a messagebox saying the
ThreadAbortExce ption 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.Threadin g

[...]

Private Sub Form1_Load(ByVa l sender As System.Object, _
ByVal e As System.EventArg s) Handles MyBase.Load

Dim currentDomain As AppDomain = AppDomain.Curre ntDomain

AddHandler currentDomain.U nhandledExcepti on, _
AddressOf UnhandledExcept ionHandler

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

Dim t As New Thread(AddressO f ThreadProc)
t.Start()
Thread.Sleep(10 00)
t.Abort()
End Sub

Private Sub UnhandledExcept ionHandler(ByVa l sender As Object, _
ByVal args As UnhandledExcept ionEventArgs)

MsgBox("Unhandl ed " & args.ExceptionO bject.GetType.T oString())
End Sub

Private Sub ThreadProc()
Try
Do
Thread.Sleep(10 0)
Loop
Catch ex As ThreadAbortExce ption
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 ThreadAbortExce ption 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 ThreadAbortExce ptions, 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 UnhandledExcept ion 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 ThreadAbortExce ption 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.Threadin g

Module Module1

Dim thr As Thread

Sub Main()
Dim currentDomain As AppDomain = AppDomain.Curre ntDomain
AddHandler currentDomain.U nhandledExcepti on, _
AddressOf UnhandledExcept ionHandler

thr = New Thread(AddressO f ThreadProc)
thr.Start()
Thread.Sleep(10 00)
thr.Abort()
End Sub

Private Sub UnhandledExcept ionHandler(ByVa l sender As Object, _
ByVal args As UnhandledExcept ionEventArgs)
Dim ex As ThreadAbortExce ption = CType(args.Exce ptionObject,
ThreadAbortExce ption)
Debug.WriteLine ("Unhandled " & ex.ToString())
System.Console. WriteLine("Unha ndled " & ex.ToString())
End Sub

Private Sub ThreadProc()
Try
Do
Thread.Sleep(10 0)
Loop
Catch ex As ThreadAbortExce ption
Debug.WriteLine ("Handled " & ex.ToString)
System.Console. WriteLine("Hand led " & 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.Threadin g.ThreadAbortEx ception: Thread was being aborted.
at System.Threadin g.Thread.Sleep( Int32 millisecondsTim eout)
at ThreadAbortExce ptionTest2.Modu le1.ThreadProc( )
in D:\My Programs\Test\T hreadAbortExcep tionTest2\Modul e1.vb:line 28
Unhandled System.Threadin g.ThreadAbortEx ception: Thread was being aborted.
at ThreadAbortExce ptionTest2.Modu le1.ThreadProc( )
in D:\My Programs\Test\T hreadAbortExcep tionTest2\Modul e1.vb:line 32
The thread '<No Name>' (0xb08) has exited with code 0 (0x0).
The program '[2952] ThreadAbortExce ptionTest2.exe' has exited with code 0
(0x0).

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

Handled System.Threadin g.ThreadAbortEx ception: Thread was being aborted.
at System.Threadin g.Thread.Sleep( Int32 millisecondsTim eout)
at ThreadAbortExce ptionTest2.Modu le1.ThreadProc( )
in D:\My Programs\Test\T hreadAbortExcep tionTest2\Modul e1.vb:line 28
The thread '<No Name>' (0xb08) has exited with code 0 (0x0).
The program '[2952] ThreadAbortExce ptionTest2.exe' has exited with code 0
(0x0).

without any unhandled exception.

Nov 21 '05 #3
ThreadAbortExce ption 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 ThreadAbortExce ption, as Luke pointed
out ThreadAbortExce ption is automatically raised again.
To prevent ThreadAbortExce ption from being automatically raised again you
need to use Thread.ResetAbo rt.
Hope this helps
Jay

"Lucvdv" <re**********@n ull.net> wrote in message
news:v6******** *************** *********@4ax.c om...
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 ThreadAbortExce ption 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 ThreadAbortExce ptions, 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 UnhandledExcept ion 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 ThreadAbortExce ption 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.Threadin g

Module Module1

Dim thr As Thread

Sub Main()
Dim currentDomain As AppDomain = AppDomain.Curre ntDomain
AddHandler currentDomain.U nhandledExcepti on, _
AddressOf UnhandledExcept ionHandler

thr = New Thread(AddressO f ThreadProc)
thr.Start()
Thread.Sleep(10 00)
thr.Abort()
End Sub

Private Sub UnhandledExcept ionHandler(ByVa l sender As Object, _
ByVal args As UnhandledExcept ionEventArgs)
Dim ex As ThreadAbortExce ption = CType(args.Exce ptionObject,
ThreadAbortExce ption)
Debug.WriteLine ("Unhandled " & ex.ToString())
System.Console. WriteLine("Unha ndled " & ex.ToString())
End Sub

Private Sub ThreadProc()
Try
Do
Thread.Sleep(10 0)
Loop
Catch ex As ThreadAbortExce ption
Debug.WriteLine ("Handled " & ex.ToString)
System.Console. WriteLine("Hand led " & 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.Threadin g.ThreadAbortEx ception: Thread was being aborted.
at System.Threadin g.Thread.Sleep( Int32 millisecondsTim eout)
at ThreadAbortExce ptionTest2.Modu le1.ThreadProc( )
in D:\My Programs\Test\T hreadAbortExcep tionTest2\Modul e1.vb:line 28
Unhandled System.Threadin g.ThreadAbortEx ception: Thread was being aborted.
at ThreadAbortExce ptionTest2.Modu le1.ThreadProc( )
in D:\My Programs\Test\T hreadAbortExcep tionTest2\Modul e1.vb:line 32
The thread '<No Name>' (0xb08) has exited with code 0 (0x0).
The program '[2952] ThreadAbortExce ptionTest2.exe' has exited with code 0
(0x0).

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

Handled System.Threadin g.ThreadAbortEx ception: Thread was being aborted.
at System.Threadin g.Thread.Sleep( Int32 millisecondsTim eout)
at ThreadAbortExce ptionTest2.Modu le1.ThreadProc( )
in D:\My Programs\Test\T hreadAbortExcep tionTest2\Modul e1.vb:line 28
The thread '<No Name>' (0xb08) has exited with code 0 (0x0).
The program '[2952] ThreadAbortExce ptionTest2.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:
ThreadAbortExce ption 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
1549
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 readily explain. The program connects to, then reads and creates a new flat old Dbase files (.DBF) stored on a network drive. The program itself is run from the very same network drive/folder.
4
2412
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 environment and classes, which should prevent the before mentioned behaviour. Does VB.NET have a setting like it? Or is there something else? Thanks in advance Frank
5
3317
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 view it in one of the windows (autos or locals, don't remember which). That doesn't appear to be the case with vb.net. I also tried using the command window to inspect "Err" but that gave nothing useful (always empty).
3
1822
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 Exception <<< Here!
3
2995
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 process finishes it causes an unhandled exception, even though both host and child process have all thread exceptions handled. Unfortunately this is making debugging a complete and utter nightmare as the application has already terminated by...
2
2189
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 Interactive voice response telephone application. An unforeseen error occurs on one phone call. You terminate that phone call politely but you don't all of a sudden hang up the phone without warning on the other 400 callers that are on line at that...
6
1672
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 override the OnLoad method to throw an exception: public partial class FaultyForm : Form ...
1
914
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
5892
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 application. /// </summary>
0
9901
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10364
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9510
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7909
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7077
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5738
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5937
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4150
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.