473,769 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rethrowing exceptions

I am implementing an asynchronous handler (similar to Control.EndInvo ke) and
if the other thread throws an exception, I catch it and rethrow it on the
current thread. How can I rethrow the exception on the current thread while
maintaining the stack trace? The best I've come up with is encapsulating
the exception as an inner exception. I would prefer to just rethrow the
exception directly -- no need for an inner exception.

How does Control.EndInvo ke handle this?

Thanks
May 6 '07 #1
4 2374
"Coder Guy" <Co******@discu ssions.microsof t.comwrote in message
news:ul******** ******@TK2MSFTN GP06.phx.gbl...
>I am implementing an asynchronous handler (similar to Control.EndInvo ke)
and if the other thread throws an exception, I catch it and rethrow it on
the current thread. How can I rethrow the exception on the current thread
while maintaining the stack trace? The best I've come up with is
encapsulatin g the exception as an inner exception. I would prefer to just
rethrow the exception directly -- no need for an inner exception.
You can rethrow the current exception writing just "throw;" instead of
"throw myNewException; ".
May 7 '07 #2
I think you missed the point. The exception is saved and rethrown on
another thread, and therefore, that won't work for me...
You can rethrow the current exception writing just "throw;" instead of
"throw myNewException; ".

May 7 '07 #3
Once you "throw ex" rather than "throw" you lose the stack trace. So,
providing the innerException as you described is probably the only way to do
what you want, unless you want to copy everything to a new exception object.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Coder Guy" wrote:
I think you missed the point. The exception is saved and rethrown on
another thread, and therefore, that won't work for me...
You can rethrow the current exception writing just "throw;" instead of
"throw myNewException; ".


May 7 '07 #4
Coder Guy <Co******@discu ssions.microsof t.comwrote:
I am implementing an asynchronous handler (similar to Control.EndInvo ke) and
if the other thread throws an exception, I catch it and rethrow it on the
current thread. How can I rethrow the exception on the current thread while
maintaining the stack trace? The best I've come up with is encapsulating
the exception as an inner exception. I would prefer to just rethrow the
exception directly -- no need for an inner exception.

How does Control.EndInvo ke handle this?
Are you sure that Control.EndInvo ke *does* handle this? Does it appear
to replace the stack trace with the original one?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 8 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
5292
by: David Turner | last post by:
Hi all I noticed something interesting while testing some RAII concepts ported from C++ in Python. I haven't managed to find any information about it on the web, hence this post. The problem is that when an exception is raised, the destruction of locals appears to be deferred to program exit. Am I missing something? Is this behaviour by design? If so, is there any reason for it? The only rationale I can think of is to speed up...
21
2236
by: dkcpub | last post by:
I'm very new to Python, but I couldn't find anything in the docs or faq about this. And I fished around in the IDLE menus but didn't see anything. Is there a tool that can determine all the exceptions that can be raised in a Python function, or in any of the functions it calls, etc.? /Dan
2
1723
by: Martin Smith | last post by:
If a class has no need for any specific clean up code in a particular catch block is there any benefit to catching and rethrowing exceptions, rather than just letting the original exception bubble up to the calling function If so what?
6
1676
by: Chris Newcombe | last post by:
Please could someone on the VC++ 7.0 compiler team (note; not 7.1) tell me if this code is handled 'correctly' (i.e. as the original poster suggests) in all cases? http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=89ed5u%248ck%40library1.airnews.net (Incase the URL doesn't make it I've copied it below.) I have a situation where I really need this technique. But the fact that Dave Abrahams wasn't sure about it very much worries...
2
4390
by: Lasse Vågsæther Karlsen | last post by:
If I got the following code: try { // something that might throw an exception } catch (Exception ex) { // Log contents of ex here throw;
5
2863
by: Steven T. Hatton | last post by:
I haven't thought about rethrowing an exception in a long time, and tried to do it the wrong way. Now I'm curious about /why/ it's wrong. My expectation was that the exception instance would simply be passed up the call stack as the same object. It would therefore behave polymorphically. Is the behavior shown below compiler-specific? I'm using gcc 4.0.2. In terms of what goes on at runtime, why does the `throw e' act as if I threw a...
15
1900
by: =?Utf-8?B?TWljaGVsIFBvc3NldGggW01DUF0=?= | last post by:
In my opinion rethrowing exceptions without providing anny extra information is a totall waste Examples : in my opinion wrong : A: Public sub DoSomeStuff() Try do it
13
1897
by: valentin tihomirov | last post by:
I do not understand something. A caught exception stack points to an argument-free 'throw' in a catch block. Meantime, it is widely known that these statements re-throw the original exceptions. I expected, therefore, that the stack points to the original 'throw e' code line.
5
6468
by: jerseycat10 | last post by:
I am trying to overcome the following hurdle. We have several exception types, derived from a "BaseException" type. Lets say SpecialException inherits from BaseException. Lets say a SpecialException is thrown, and handled by try/catch that is catching "BaseException". If I retry to throw this exception, I get a core dump.
0
6501
RedSon
by: RedSon | last post by:
Chapter 3: What are the most common Exceptions and what do they mean? As we saw in the last chapter, there isn't only the standard Exception, but you also get special exceptions like NullPointerException or ArrayIndexOutOfBoundsException. All of these extend the basic class Exception. In general, you can sort Exceptions into two groups: Checked and unchecked Exceptions. Checked Exceptions are checked by the compiler at compilation time. Most...
0
9422
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10208
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
9987
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
8867
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
7404
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
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5294
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...
1
3952
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3558
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.