Application.ThreadException

R

Rakesh

Hi,

I am catching unhandled exception by attached an event
handler on the Application.ThreadException event. This
will ensure that I could handle all uncaught exceptions
on the main thread.

But, suppose I create a new thread within my app and an
unhandled exception is generated in that thread, will
that exception be caught here?

Thanks in advance,
Rakesh
 
R

Rakesh

Hi Jan,

Thanks!

So do u mean that we have to go about the same old way of
try-catch in this case? Any other way?

Rakesh
 
D

Dave

You can attach a handler to the AppDomain.CurrentDomain.UnhandledException
event, but be aware that one of its limitations is that it will only work
for the default appdomain.

If you create the thread yourself then you should put a try-catch block
within the thread's start function to ensure that you will catch and handle
all exceptions. For the main thread and threads that originate in unmanaged
code, an unhandled exception will terminate the app. For threads you create
manually, threadpool threads, and finalizer threads, even though your
unhandled exception handler is called, the runtime will catch and swallow
all exceptions; the IsTerminating field in the UnhandledExceptionEventArgs
is set to false.
 
R

Rakesh

Hi Dave,

I want to know if i got you right:

So if i have only one appdomain and i attach an event
handler in the
AppDomain.CurrentDomain.UnhandledException, will that
ensure that I can handle all the uncaught exceptions
regardless of whichever thread they are in?

Rakesh
 
C

codymanix

The only way to catch all exceptions in every thread is to enclose every
thread proc in a try/catch clause.
 
J

Jan Tielens

I think that is what Dave meant. But be awere it's only in the current app
domain!
 
G

Guest

Could have a launcher program that does a process start and catches the
exception there?
 
B

Bijoy Thulasidharan

try this
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(Domain_Exception_Handler);

it should handle all the exceptions that occur in the app domain no matter
on which thread.
 
D

Dave

You aren't really catching them; think of it as a notification that one
occurred. It gives you an opportunity to provide some application-wide
policies (log it, notify someone via email, terminate the process, etc). If
you want to programmatically recover from it then you must wrap the callsite
throwing the exception in a try-catch block.

If you look at the fields in the UnhandledExceptionEventArgs the unhandled
exception handler gets, one of them is the flag I mentioned called
IsTerminating. This is a read-only property, which means that the runtime
itself is determining what will happen next and that it is too late to
recover from it.

If you hook the AppDomain.CurrentDomain.UnhandledException you will be
notified that the exception occurred but you will not be able to recover
from it within this method. I don't know what functionality you want to
provide so I do not know if this is suitable or not for you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top