Cannot catch Exception in a thread

D

David

I am having a bit of a problem with catching an exception within a thread.

Here is the scenario:

I have a Windows Form.
I create a new thread.
This new thread calls a method in another DLL that catches an expected exception.
The method in the DLL throws the exception back out.
When in debug mode, the process just exits.


Here is some sample code:

public class myform : system.windows.form {
try {
ThreadClass cl = new ThreadClass();
Thread nThread = new Thread(new ThreadStart(cl.runprocess));

nThread.IsBackground = true;
nThread.Start();
}
catch(Exception ex) {
...
//Exception from thread is never caught
...
}
}



public class ThreadClass() {
public ThreadClass() {}

public void runprocess() {
try {
newDLL nDLL = new newDLL();
nDLL.DoSomething(); //<--- Exception occurrs and is rethrown in
// this method.
...
//Do more stuff //<-- never makes it to here
...
}
catch(Exception ex) {
//Do stuff here //<-- never makess it to here
}
}

}

The thread state after the exception occurrs is always ThreadState.Running.

What am I misssing?

-David
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi David,
try {
ThreadClass cl = new ThreadClass();
Thread nThread = new Thread(new ThreadStart(cl.runprocess));

nThread.IsBackground = true;
nThread.Start();
}
catch(Exception ex) {
...
//Exception from thread is never caught

Exceptions from other threads won't be caught here.
public class ThreadClass() {
public ThreadClass() {}

public void runprocess() {
try {
newDLL nDLL = new newDLL();
nDLL.DoSomething(); //<--- Exception occurrs and is rethrown in
// this method.
...
//Do more stuff //<-- never makes it to here
...
}
catch(Exception ex) {
//Do stuff here //<-- never makess it to here

But at this point the exception should be caught, provided it is
a managed exception derived from the System.Exception class.

You can also add a handler to the AppDomain.UnhandledException
event and check to see whether it makes it to there.
Try also adding a handler for the Application.ThreadException event.
 
A

Andreas Huber

I am having a bit of a problem with catching an exception within a thread.

Here is the scenario:

I have a Windows Form.
I create a new thread.
This new thread calls a method in another DLL that catches an expected exception.
The method in the DLL throws the exception back out.
When in debug mode, the process just exits.


Here is some sample code:

public class myform : system.windows.form {
try {
ThreadClass cl = new ThreadClass();
Thread nThread = new Thread(new ThreadStart(cl.runprocess));

nThread.IsBackground = true;
nThread.Start();
}
catch(Exception ex) {
...
//Exception from thread is never caught
...
}
}



public class ThreadClass() {
public ThreadClass() {}

public void runprocess() {
try {
newDLL nDLL = new newDLL();
nDLL.DoSomething(); //<--- Exception occurrs and is rethrown in
// this method.
...
//Do more stuff //<-- never makes it to here
...
}
catch(Exception ex) {
//Do stuff here //<-- never makess it to here
}
}

}

The thread state after the exception occurrs is always ThreadState.Running.

What am I misssing?

Thrown exceptions are stack-bound objects. Since every thread has its
own stack, an exception thrown in thread A cannot suddenly appear in
thread B.

If you want to get an exception that was thrown in thread A in thread
B, thread A needs to catch the exception and store it somewhere before
it terminates. Afterwards thread B can collect and rethrow the
exception.
Luckily, all this is readily available in the framework with
asynchronous delegates (MSDN:
http://msdn.microsoft.com/library/d...conasynchronousdelegatesprogrammingsample.asp).

HTH,

Andreas
 
G

Guest

Hi David

I have reviewed your post, I will do some research on it

I will reply to you ASAP

Thanks for your understanding

Best regards
Jeffrey Ta
Microsoft Online Partner Suppor
Get Secure! - www.microsoft.com/securit
This posting is provided "as is" with no warranties and confers no rights
 
D

Dave

Something is inaccurate in what you are seeing or reporting. In v1.0 and
v1.1 of the runtime an unhandled exception will only cause the app to exit
if it occurs on the main thread or a thread that originates in unmanaged
code. Unhandled exceptions on manually created threads, threadpool threads,
etc., will not cause the runtime to exit. The only exception to this is if
the exception thrown is unrecoverable, such as ExecutionEngineException,
StackOverflowException, or OutOfMemoryException.

It is posible that the DLL that you are calling into is itself using
additional threads that are throwing exceptions. If that is the situation
then the try-catch handler you wrap the call to nDLL.DoSomething() in will
not catch the exception.

I'd take Dimitry's advice and add a handler to the
AppDomain.UnhandledException event as well as the
Application.ThreadException event. You wont be able to correct the problem
from there but you should be able to get more information from it.

If you keep having problems you will need to provde more precise information
on what is reported on the exception.
 
G

Guest

Hi David,

I think Dave's reply really helpful.

Does the community's reply make sense to you?

If you still have anything unclear, please feel free to post, I will help
you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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