473,387 Members | 1,548 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,387 software developers and data experts.

about AppDomain.UnhandledException

Quick question about the UnhandledException event and associated Handler.

I just implemented this handler for the first time, and am surprised that it
this event is being raised for an exception that I have handled.
I have the following:

private void Load()
{
try
{
Load(true);
}
catch (ThreadAbortException ex)
{
Debug.WriteLine("Thread Abort Exception!!!");
/* Empty Catch should eat the exception? */
}
catch (Exception ex)
{
throw;
}
}

but each time I get a ThreadAbortException, my UnhandledException handler is
being fired from this method. Why and what do I have to do to prevent this
behaviour?
Nov 17 '05 #1
5 1930
ThreadAborts are special exceptions...even though they are caught in the
catch handler, when the code exits that catch handler the runtime rethrows
the exception until either the abort has been explicitly reset
(Thread.ResetAbort) or until the thread has unwound to the top of the stack
(TOS) and the thread has been terminated.

If you do not want to get the UE event then you must reset the abort within
the catch handler for the ThreadAbort exception. If you put the
Thread.ResetAbort anywhere after that (such as a finally block) you will
still get the UE notification. This is because the catch handlers are
evaluated all the way up the stack until it reaches TOS. If it has not found
a suitable catch handler by then you immediately get the UE event. It then
goes back and executes finally blocks, so if you put the reset in a finally
block the reset will occur after the UE event was fired.

The behavior of the abort is fairly well documented even if the timing is
not obvious.
"John Richardson" <j3**********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Quick question about the UnhandledException event and associated Handler.

I just implemented this handler for the first time, and am surprised that
it this event is being raised for an exception that I have handled.
I have the following:

private void Load()
{
try
{
Load(true);
}
catch (ThreadAbortException ex)
{
Debug.WriteLine("Thread Abort Exception!!!");
/* Empty Catch should eat the exception? */
}
catch (Exception ex)
{
throw;
}
}

but each time I get a ThreadAbortException, my UnhandledException handler
is being fired from this method. Why and what do I have to do to prevent
this behaviour?

Nov 17 '05 #2
thanks for the detailed response. I was a bit confused by this. This is my
first forray into threading with C# as well... :) I should have read more
before using them (threads).

"David Levine" <no******************@wi.rr.com> wrote in message
news:uA**************@TK2MSFTNGP09.phx.gbl...
ThreadAborts are special exceptions...even though they are caught in the
catch handler, when the code exits that catch handler the runtime rethrows
the exception until either the abort has been explicitly reset
(Thread.ResetAbort) or until the thread has unwound to the top of the
stack (TOS) and the thread has been terminated.

If you do not want to get the UE event then you must reset the abort
within the catch handler for the ThreadAbort exception. If you put the
Thread.ResetAbort anywhere after that (such as a finally block) you will
still get the UE notification. This is because the catch handlers are
evaluated all the way up the stack until it reaches TOS. If it has not
found a suitable catch handler by then you immediately get the UE event.
It then goes back and executes finally blocks, so if you put the reset in
a finally block the reset will occur after the UE event was fired.

The behavior of the abort is fairly well documented even if the timing is
not obvious.
"John Richardson" <j3**********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Quick question about the UnhandledException event and associated Handler.

I just implemented this handler for the first time, and am surprised that
it this event is being raised for an exception that I have handled.
I have the following:

private void Load()
{
try
{
Load(true);
}
catch (ThreadAbortException ex)
{
Debug.WriteLine("Thread Abort Exception!!!");
/* Empty Catch should eat the exception? */
}
catch (Exception ex)
{
throw;
}
}

but each time I get a ThreadAbortException, my UnhandledException handler
is being fired from this method. Why and what do I have to do to prevent
this behaviour?


Nov 17 '05 #3
I've seen your name around a couple of threading related posts. Do you know
a good online/hard copy reference I might read to understand the subtleties?
I'm not 100% about the theory nor implementation about threading nor how
synchronization works in C#. I'd like to know more about best practices.
"David Levine" <no******************@wi.rr.com> wrote in message
news:uA**************@TK2MSFTNGP09.phx.gbl...
ThreadAborts are special exceptions...even though they are caught in the
catch handler, when the code exits that catch handler the runtime rethrows
the exception until either the abort has been explicitly reset
(Thread.ResetAbort) or until the thread has unwound to the top of the
stack (TOS) and the thread has been terminated.

If you do not want to get the UE event then you must reset the abort
within the catch handler for the ThreadAbort exception. If you put the
Thread.ResetAbort anywhere after that (such as a finally block) you will
still get the UE notification. This is because the catch handlers are
evaluated all the way up the stack until it reaches TOS. If it has not
found a suitable catch handler by then you immediately get the UE event.
It then goes back and executes finally blocks, so if you put the reset in
a finally block the reset will occur after the UE event was fired.

The behavior of the abort is fairly well documented even if the timing is
not obvious.
"John Richardson" <j3**********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Quick question about the UnhandledException event and associated Handler.

I just implemented this handler for the first time, and am surprised that
it this event is being raised for an exception that I have handled.
I have the following:

private void Load()
{
try
{
Load(true);
}
catch (ThreadAbortException ex)
{
Debug.WriteLine("Thread Abort Exception!!!");
/* Empty Catch should eat the exception? */
}
catch (Exception ex)
{
throw;
}
}

but each time I get a ThreadAbortException, my UnhandledException handler
is being fired from this method. Why and what do I have to do to prevent
this behaviour?


Nov 17 '05 #4
That's a very broad topic. C#/.NET simplifies some of it, but for a real
understanding of how threading, synchronization, and exceptions work you
really need to understand how these work under Win32, since .NET is layered
on top of that, and many of the concepts are the same.

For base threading and synchronization, the books by Richter are a great
place to start.

Exceptions are built on top of the Windows Structured Exception Handling
(SEH) mechanism. Richter does a pretty good job of describing this too.
Exceptions are also integral to issues such as reliability, finalization,
finalizers, deterministic finalization, etc., since these are impacted by
SEH and exceptions.

Unfortunately, there is no single source you can go to for a complete
discussion of exceptions under .NET - pieces of it are scattered all over.

For information on best practices and strategies it is even less
satisfactory - MSFT and others have published guidelines, but I don't agree
with all the recommendations.

These links have good information on the basics of how the mechanism works,
both fot win32 and for .net.

http://www.microsoft.com/msj/0197/Ex...Exception.aspx
http://blogs.msdn.com/cbrumme/archiv.../01/51524.aspx

The CLI specifications also have a good description of the .NET model.
"John Richardson" <j3**********@hotmail.com> wrote in message
news:Or*************@TK2MSFTNGP15.phx.gbl...
I've seen your name around a couple of threading related posts. Do you
know a good online/hard copy reference I might read to understand the
subtleties? I'm not 100% about the theory nor implementation about
threading nor how synchronization works in C#. I'd like to know more
about best practices.
"David Levine" <no******************@wi.rr.com> wrote in message
news:uA**************@TK2MSFTNGP09.phx.gbl...
ThreadAborts are special exceptions...even though they are caught in the
catch handler, when the code exits that catch handler the runtime
rethrows the exception until either the abort has been explicitly reset
(Thread.ResetAbort) or until the thread has unwound to the top of the
stack (TOS) and the thread has been terminated.

If you do not want to get the UE event then you must reset the abort
within the catch handler for the ThreadAbort exception. If you put the
Thread.ResetAbort anywhere after that (such as a finally block) you will
still get the UE notification. This is because the catch handlers are
evaluated all the way up the stack until it reaches TOS. If it has not
found a suitable catch handler by then you immediately get the UE event.
It then goes back and executes finally blocks, so if you put the reset in
a finally block the reset will occur after the UE event was fired.

The behavior of the abort is fairly well documented even if the timing is
not obvious.
"John Richardson" <j3**********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Quick question about the UnhandledException event and associated
Handler.

I just implemented this handler for the first time, and am surprised
that it this event is being raised for an exception that I have handled.
I have the following:

private void Load()
{
try
{
Load(true);
}
catch (ThreadAbortException ex)
{
Debug.WriteLine("Thread Abort Exception!!!");
/* Empty Catch should eat the exception? */
}
catch (Exception ex)
{
throw;
}
}

but each time I get a ThreadAbortException, my UnhandledException
handler is being fired from this method. Why and what do I have to do
to prevent this behaviour?



Nov 17 '05 #5
thanks again.
"David Levine" <no******************@wi.rr.com> wrote in message
news:up**************@TK2MSFTNGP10.phx.gbl...
That's a very broad topic. C#/.NET simplifies some of it, but for a real
understanding of how threading, synchronization, and exceptions work you
really need to understand how these work under Win32, since .NET is
layered on top of that, and many of the concepts are the same.

For base threading and synchronization, the books by Richter are a great
place to start.

Exceptions are built on top of the Windows Structured Exception Handling
(SEH) mechanism. Richter does a pretty good job of describing this too.
Exceptions are also integral to issues such as reliability, finalization,
finalizers, deterministic finalization, etc., since these are impacted by
SEH and exceptions.

Unfortunately, there is no single source you can go to for a complete
discussion of exceptions under .NET - pieces of it are scattered all over.

For information on best practices and strategies it is even less
satisfactory - MSFT and others have published guidelines, but I don't
agree with all the recommendations.

These links have good information on the basics of how the mechanism
works, both fot win32 and for .net.

http://www.microsoft.com/msj/0197/Ex...Exception.aspx
http://blogs.msdn.com/cbrumme/archiv.../01/51524.aspx

The CLI specifications also have a good description of the .NET model.
"John Richardson" <j3**********@hotmail.com> wrote in message
news:Or*************@TK2MSFTNGP15.phx.gbl...
I've seen your name around a couple of threading related posts. Do you
know a good online/hard copy reference I might read to understand the
subtleties? I'm not 100% about the theory nor implementation about
threading nor how synchronization works in C#. I'd like to know more
about best practices.
"David Levine" <no******************@wi.rr.com> wrote in message
news:uA**************@TK2MSFTNGP09.phx.gbl...
ThreadAborts are special exceptions...even though they are caught in the
catch handler, when the code exits that catch handler the runtime
rethrows the exception until either the abort has been explicitly reset
(Thread.ResetAbort) or until the thread has unwound to the top of the
stack (TOS) and the thread has been terminated.

If you do not want to get the UE event then you must reset the abort
within the catch handler for the ThreadAbort exception. If you put the
Thread.ResetAbort anywhere after that (such as a finally block) you will
still get the UE notification. This is because the catch handlers are
evaluated all the way up the stack until it reaches TOS. If it has not
found a suitable catch handler by then you immediately get the UE event.
It then goes back and executes finally blocks, so if you put the reset
in a finally block the reset will occur after the UE event was fired.

The behavior of the abort is fairly well documented even if the timing
is not obvious.
"John Richardson" <j3**********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Quick question about the UnhandledException event and associated
Handler.

I just implemented this handler for the first time, and am surprised
that it this event is being raised for an exception that I have
handled.
I have the following:

private void Load()
{
try
{
Load(true);
}
catch (ThreadAbortException ex)
{
Debug.WriteLine("Thread Abort Exception!!!");
/* Empty Catch should eat the exception? */
}
catch (Exception ex)
{
throw;
}
}

but each time I get a ThreadAbortException, my UnhandledException
handler is being fired from this method. Why and what do I have to do
to prevent this behaviour?



Nov 17 '05 #6

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

Similar topics

0
by: Marcio Izepe | last post by:
I am with a problem that I do not obtain to understand. I put a method in the UnhandledException event of AppDomain to capture and log exceptions, however it does not pass for it when I am working...
2
by: Martin Lapierre | last post by:
How can I remove the default exception handler handler? When adding a custom handler, I can't get rid of the VS.NET unhandled exception dialog. Ex: AppDomain currentDomain =...
1
by: Kimmo Laine | last post by:
Hi, does the AppDomain.UnhandledException also work when you use it in Windows Service - i was unable to get the handler called when i generated an exception. thx Kimmo Laine
2
by: guy | last post by:
In my application I have an event handler to catch unhandled exceptions, as per the MSDN documentation, however when an unhandled exception is thrown I get the Dialogue first, then the event...
2
by: Chris Dunaway | last post by:
I am attempting to use the AppDomain.UnhandledException event in a Windows Forms app and also in a Windows Service. But the event doesn't seem to be called. In a Windows Forms app, the event IS...
9
by: Gustaf | last post by:
I'm confused about structured error handling. The following piece of code is a simplification of a class library I'm working on. It works, and it does what I want, but I'm still not convinced that...
4
by: Steve | last post by:
I have read a couple articles online, read my Jesse Liberty book but I am still confused as to just what the best practices are for using exceptions. I keep changing how I'm working with them and...
1
by: pack | last post by:
What're the meaning of "App domain" and "Current Domain" in appdomain.CurrentDomain.UnhandledException?
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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
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?
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
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,...

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.