473,663 Members | 2,838 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re-Throwing Exceptions

Hey,
I use the following code:
try
{
//Some code that has an exception
}
catch (Exception ex)
{
if (ex.innerExcept ion!=null) {throw ex.innerExcepti on;}
}

All works well, but the problem is that the exception's stacktrace is
changed because of the throw!

Is there a way to throw an exception WITHOUT changing it's stacktrace?
Thanks ahead

--sternr

Jun 1 '06 #1
11 2761
sternr wrote:
Hey,
I use the following code:
try
{
//Some code that has an exception
}
catch (Exception ex)
{
if (ex.innerExcept ion!=null) {throw ex.innerExcepti on;}
}

All works well, but the problem is that the exception's stacktrace is
changed because of the throw!

Is there a way to throw an exception WITHOUT changing it's stacktrace?
Thanks ahead

--sternr

Why the stacktrace should be left unchanged in case of re-throwing the
same exception? When its thrown again the stacktrace should be updated.
But if you rethrow the outer exception (ex in your example) instead of
innerException, the inner exceptions's stack trace will be unchanged.
rg
Jun 1 '06 #2
just simply throw; will work. it's rather pointless IMO to catch an
exception and rethrow the inner exception? Why would that be helpful?

--

_______________ _________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
-------------------------------------------------------

"sternr" <St****@gmail.c om> wrote in message
news:11******** **************@ j55g2000cwa.goo glegroups.com.. .
Hey,
I use the following code:
try
{
//Some code that has an exception
}
catch (Exception ex)
{
if (ex.innerExcept ion!=null) {throw ex.innerExcepti on;}
}

All works well, but the problem is that the exception's stacktrace is
changed because of the throw!

Is there a way to throw an exception WITHOUT changing it's stacktrace?
Thanks ahead

--sternr

Jun 1 '06 #3
<"Alvin Bruney" <www.lulu.com/owc>> wrote:
just simply throw; will work.
No, that will rethrow the *outer* exception.
it's rather pointless IMO to catch an
exception and rethrow the inner exception? Why would that be helpful?


There are cases where a known level of wrapping occurs, and you want to
get rid of that so that the real exception is reported immediately. For
instance, if you're creating an instance of a plugin using reflection,
it might make sense to catch TargetInvocatio nException and rethrow the
inner exception. The TargetInvocatio nException should basically be
ignored by anyone looking at a stack trace anyway, and it's just extra
clutter.

--
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
Jun 4 '06 #4
> There are cases where a known level of wrapping occurs, and you want to
get rid of that so that the real exception is reported immediately. For
instance, if you're creating an instance of a plugin using reflection,
it might make sense to catch TargetInvocatio nException and rethrow the
inner exception. The TargetInvocatio nException should basically be
ignored by anyone looking at a stack trace anyway, and it's just extra
clutter.
hmm, in that case - and it is not the usual or the typical case - it makes
more sense to throw your own exception because you are actually applying
logic to the original exception object. Such logic should be localized to
prevent code bloat and improve source code maintainability .

--

_______________ _________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
-------------------------------------------------------

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... <"Alvin Bruney" <www.lulu.com/owc>> wrote:
just simply throw; will work.


No, that will rethrow the *outer* exception.
it's rather pointless IMO to catch an
exception and rethrow the inner exception? Why would that be helpful?


There are cases where a known level of wrapping occurs, and you want to
get rid of that so that the real exception is reported immediately. For
instance, if you're creating an instance of a plugin using reflection,
it might make sense to catch TargetInvocatio nException and rethrow the
inner exception. The TargetInvocatio nException should basically be
ignored by anyone looking at a stack trace anyway, and it's just extra
clutter.

--
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

Jun 4 '06 #5
<"Alvin Bruney" <www.lulu.com/owc>> wrote:
There are cases where a known level of wrapping occurs, and you want to
get rid of that so that the real exception is reported immediately. For
instance, if you're creating an instance of a plugin using reflection,
it might make sense to catch TargetInvocatio nException and rethrow the
inner exception. The TargetInvocatio nException should basically be
ignored by anyone looking at a stack trace anyway, and it's just extra
clutter.


hmm, in that case - and it is not the usual or the typical case - it makes
more sense to throw your own exception because you are actually applying
logic to the original exception object. Such logic should be localized to
prevent code bloat and improve source code maintainability .


I would say that the original exception is the most reasonable one -
it's what we *would* have got if we'd been able to call the constructor
directly instead of having to use reflection. Why introduce an extra
level of indirection with no benefit?

I agree it's a rare case, but it can happen.

--
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
Jun 4 '06 #6
Hey Jon,
The scenario you mentioned before is exactly the reason for my
question:
I call a method using reflection, and want to be able to catch the
exception
Thrown from this method without the wrapping TargetInvocatio nException.
The solution I did is as follows:

catch (Exception ex)
Type type;
Assembly assembly;
object newEx;
type=ex.GetType ();
assembly=Assemb ly.GetAssembly( ex);
newEx=assembly. CreateInstance( ex.FullName,fal se,BindingFlags .CreateInstance ,null,new
object[]{ex.InnerExcept ion.Message,ex. InnerException} ,System.Globali zation.CultureI nfo.CurrentCult ure,null);
}
Enjoy ;)

Jon wrote:
<"Alvin Bruney" <www.lulu.com/owc>> wrote:
There are cases where a known level of wrapping occurs, and you want to
get rid of that so that the real exception is reported immediately. For
instance, if you're creating an instance of a plugin using reflection,
it might make sense to catch TargetInvocatio nException and rethrow the
inner exception. The TargetInvocatio nException should basically be
ignored by anyone looking at a stack trace anyway, and it's just extra
clutter.


hmm, in that case - and it is not the usual or the typical case - it makes
more sense to throw your own exception because you are actually applying
logic to the original exception object. Such logic should be localized to
prevent code bloat and improve source code maintainability .


I would say that the original exception is the most reasonable one -
it's what we *would* have got if we'd been able to call the constructor
directly instead of having to use reflection. Why introduce an extra
level of indirection with no benefit?

I agree it's a rare case, but it can happen.

--
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


Jun 7 '06 #7
sternr <St****@gmail.c om> wrote:
The scenario you mentioned before is exactly the reason for my
question:
I call a method using reflection, and want to be able to catch the
exception
Thrown from this method without the wrapping TargetInvocatio nException.
The solution I did is as follows:


Why are you creating a *new* instance of the inner exception, rather
than just using:

throw ex.InnerExcepti on;

?

That would be a lot neater, and wouldn't rely on there being a
constructor with (string, Exception) parameters for the type of
exception thrown.

--
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
Jun 8 '06 #8

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
sternr <St****@gmail.c om> wrote:
The scenario you mentioned before is exactly the reason for my
question:
I call a method using reflection, and want to be able to catch the
exception
Thrown from this method without the wrapping TargetInvocatio nException.
The solution I did is as follows:


Why are you creating a *new* instance of the inner exception, rather
than just using:

throw ex.InnerExcepti on;

?

That would be a lot neater, and wouldn't rely on there being a
constructor with (string, Exception) parameters for the type of
exception thrown.


There are side effects to the throw statement - it resets the stack trace so
you lose the original stack. There's no way to avoid this, so unless that
is a desired side-effect I would use a different technique. I find it more
useful to wrap the innerexception in some other exception to add context and
preserve the stack.
Jun 8 '06 #9
David Levine <Sn************ *************@w i.rr.com> wrote:
Why are you creating a *new* instance of the inner exception, rather
than just using:

throw ex.InnerExcepti on;

?

That would be a lot neater, and wouldn't rely on there being a
constructor with (string, Exception) parameters for the type of
exception thrown.


There are side effects to the throw statement - it resets the stack trace so
you lose the original stack. There's no way to avoid this, so unless that
is a desired side-effect I would use a different technique. I find it more
useful to wrap the innerexception in some other exception to add context and
preserve the stack.


I agree that there are side-effects which are undesirable in this case
(as per the first message), but the OP's proposed "solution" doesn't
seem to fix this, it just introduces complexity and constraints as far
as I can see.

--
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
Jun 8 '06 #10

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

Similar topics

1
4288
by: Nel | last post by:
I have a question related to the "security" issues posed by Globals ON. It is good programming technique IMO to initialise variables, even if it's just $foo = 0; $bar = ""; Surely it would be better to promote better programming than rely on PHP to compensate for lazy programming?
4
6418
by: Craig Bailey | last post by:
Anyone recommend a good script editor for Mac OS X? Just finished a 4-day PHP class in front of a Windows machine, and liked the editor we used. Don't recall the name, but it gave line numbers as well as some color coding, etc. Having trouble finding the same in an editor that'll run on OS X. -- Floydian Slip(tm) - "Broadcasting from the dark side of the moon"
1
4090
by: Chris | last post by:
Sorry to post so much code all at once but I'm banging my head against the wall trying to get this to work! Does anyone have any idea where I'm going wrong? Thanks in advance and sorry again for adding so much code... <TABLE border="1" bordercolor="#000000" cellspacing="0"> <TR>
11
3997
by: James | last post by:
My form and results are on one page. If I use : if ($Company) { $query = "Select Company, Contact From tblworking Where ID = $Company Order By Company ASC"; }
4
18522
by: Alan Walkington | last post by:
Folks: How can I get an /exec'ed/ process to run in the background on an XP box? I have a monitor-like process which I am starting as 'exec("something.exe");' and, of course the exec function blocks until something.exe terminates. Just what I /don't/ want. (Wouldn't an & be nice here! Sigh) I need something.exe to disconnect and run in the background while I
1
3695
by: John Ryan | last post by:
What PHP code would I use to check if submitted sites to my directory actually exist?? I want to use something that can return the server code to me, ie HTTP 300 OK, or whatever. Can I do this with sockets??
10
4212
by: James | last post by:
What is the best method for creating a Web Page that uses both PHP and HTML ? <HTML> BLA BLA BLA BLA BLA
8
4409
by: Beowulf | last post by:
Hi Guru's, I have a query regarding using PHP to maintain a user profiles list. I want to be able to have a form where users can fill in their profile info (Name, hobbies etc) and attach an image, which will upload the record to a mySql db so users can then either view all profiles or query.. I.e. show all males in UK, all femails over 35 etc. Now, I'm not asking for How to do this but more what would be the best way? I've looked at...
1
3630
by: joost | last post by:
Hello, I'm kind of new to mySQL but more used to Sybase/PHP What is illegal about this query or can i not use combined query's in mySQL? DELETE FROM manufacturers WHERE manufacturers_id NOT IN ( SELECT manufacturers_id FROM products )
3
3473
by: presspley | last post by:
I have bought the book on advanced dreamweaver and PHP recently. I have installed MySQL and PHP server but am getting an error on the $GET statement show below. It says there is a problem with the variable $GET but $GET is not a variable, I thought it came from the page that calls the PHP file? if(($_GET)==""){ this gets an error
0
8436
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
8345
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
8858
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...
0
8771
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6186
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
4182
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
4349
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.