473,606 Members | 2,101 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.IsBackg round = 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.DoSomethin g(); //<--- 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.Run ning.

What am I misssing?

-David
Nov 15 '05 #1
5 27374
Hi David,
try {
ThreadClass cl = new ThreadClass();
Thread nThread = new Thread(new ThreadStart(cl. runprocess));

nThread.IsBackg round = 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.DoSomethin g(); //<--- 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.Exceptio n class.

You can also add a handler to the AppDomain.Unhan dledException
event and check to see whether it makes it to there.
Try also adding a handler for the Application.Thr eadException 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********@hotm ail.com (David) wrote in message news:<81******* *************** ****@posting.go ogle.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.IsBackg round = 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.DoSomethin g(); //<--- 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.Run ning.

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 ExecutionEngine Exception,
StackOverflowEx ception, or OutOfMemoryExce ption.

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.DoSomethin g() in will
not catch the exception.

I'd take Dimitry's advice and add a handler to the
AppDomain.Unhan dledException event as well as the
Application.Thr eadException 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********@hot mail.com> wrote in message
news:81******** *************** ***@posting.goo gle.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.IsBackg round = 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.DoSomethin g(); //<--- 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.Run ning.
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
5996
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 leave the form to go back to the originating screen, I get a "The page cannot be displayed" screen for "Cannot find server or DNS Error" "Internet Explorer". You know the page - it starts with "The page you are looking for is currently...
7
5983
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
6301
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 FileSteam, who is trying to flush and close the stream, but the disk is full, so the flush fails and another exception is thrown from another (GC) thread. An ugly solution would be to use GC.SuppressFinalize(fs). Is there a better solution or good...
5
11078
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 can tell, is it possible to do the same with a console application? Simon.
5
3388
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 operations are all within Try Catches to help me locate the problem and the origination of any problem by specifiying an error code in the "Catch" section. So theoretically this shouldn't have been possible (at which point we all say "yeah,...
3
1811
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 Exception <<< Here!
3
3911
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
6647
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) Location: class Week5MortgageGUI Week5MortgageLogic allint = logic.allInterest(amount, term, rate); Week5MortgageGUI.java:152:cannot find symbol symbol: method allInterest(double,double,double) Location: class Week5MortgageGUI
3
3175
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 System.Thread.Timer for each to control their timeout gestion, in my "main program", I start the "Scenario" and "Step", and I do something between "StepStart" and "StepStop", when "Scenario" or "Step" timeout expired, they raise an "exception", then my "main...
0
8010
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8433
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8084
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8300
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6761
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5963
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3922
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3969
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1550
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.