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

Cannot catch Exception in a thread

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
5 27359
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
go********@hotmail.com (David) wrote in message news:<81**************************@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
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
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" <go********@hotmail.com> wrote in message
news:81**************************@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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bob | last post by:
I am displaying a form with a datagrid populated from a select of database records which now exceed 12,000 when I select them all. The form displays just fine with all 12,000+ entries but when I...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
by: Muki Rapp | last post by:
Hi! In the example below, once the media is full, the FileSteam.WriteByte throws an exception and the code is designed to handle it. However, when the GC is invoked, it calls the Finalize of...
5
by: Simon Johnson | last post by:
Recently, a thread appeared which asked how to create a "catch all" method for when an exception occurs within a program. The solution given only works for Windows Forms applications, as far as i...
5
by: Simon Tamman {Uchiha Jax} | last post by:
Now this is bugging me. I just released software for a client and they have reported an unhandled stack overflow exception. My first concern is that the entirity of the UI and any threaded...
3
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...
3
by: thomson | last post by:
Hi All, Are there any sort of exceptions that cannot be caught by the Runtime, Any Insights? Thanks in Advance thomson
2
by: karinmorena | last post by:
I'm having 4 errors, I'm very new at this and I would appreciate your input. The error I get is: Week5MortgageGUI.java:151:cannot find symbol symbol: method allInterest(double,double,double)...
3
by: yeye.yang | last post by:
hey everybody Does everybody can help me or give me some advise for the cross thread exception catch Here is what I want to do: I have 2 classes "Scenario" and "Step", which have a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
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,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.