388,637 Members | 5,952 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 388,637 IT Pros & Developers. It's quick & easy.

Cannot catch Exception in a thread

David
P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


Dmitriy Lapshin [C# / .NET MVP]
P: n/a
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.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Nov 15 '05 #2

Andreas Huber
P: n/a
goodoldave@hotmail.com (David) wrote in message news:<814dd91d.0402122233.14b86197@posting.google. com>...
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/de...ingsample.asp).

HTH,

Andreas
Nov 15 '05 #3

Jeffrey Tan[MSFT]
P: n/a
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

Nov 15 '05 #4

Dave
P: n/a
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.

"David" <goodoldave@hotmail.com> wrote in message
news:814dd91d.0402122233.14b86197@posting.google.c om...
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

Nov 15 '05 #5

Jeffrey Tan[MSFT]
P: n/a

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.

Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.