I want to "rethrow" an exception so as not to lose the original stack
trace information. I understood that I could use throw, like:
try {
DoIt();
}
catch (Exception ex) {
DoSomeException Stuff(ex);
throw;
}
However, this apparently does NOT preserve the stack trace--it loses it
just like using throw ex; does! I am currently using .NET 1.1 SP1. Is
this a known bug? Why do so many people report that it works for them?
Have they actually tried it?
Why would a bug like this exist in v. 1.1 SP1?
Can anyone help shed some light on this?
Thanks in advance.
Nov 16 '05
12 4348
There are as many reasons to catch an exception at a lower level as
there are programs. (i.e. I can't think of all reasons, but there are
many.)
One is logging the exception.
Stuffing an inner exception into a newly thrown one isn't always the
most elegant solution. Logging is a case in point--I may want to note
the exception and simply pass it on up. I wouldn't want to wrap it in
another exception just because I logged it.
danielshanyf... @yahoo.com wrote: Gentlemen,
There seem to be a lot of experts replying to this thread so I will
add my own question. If you wanted to get the same exception at a higher level, why would you catch it at all?
for example (modifying the previous code):
using System;
class Test { static void Main() { try { DoSomething(); } //handle all exceptions without bias on moral behavior catch (Exception e) { Console.WriteLi ne (e); } }
static void DoSomething() { try { DoSomethingNaug hty(); } catch (NaughtyExcepti on) { //handle the offending problem } //let all of the less ethicly challenged exceptions bubble up the the calling Main() }
static void DoSomethingNaug hty() { string x = null; bool y = x.Equals("hello "); } }
If I wanted to do some intermediate processing in the routine and
then still post an error to the calling method, I would throw a new exception (of whatever type) with the original exception as the inner exception. Does stuffing the old exception into the new still mess
up the stack?
Thanks, Dan
GoogleNewsReade rMan,
Isn't that a matter of perspective?
The stack trace below(above?) DoSomething is preserved.
The stack trace within DoSomething is the line that threw the exception (The
throw itself).
The stack trace above(below?) DoSomething is maintained.
Although I can see the flip side also, if there were multiple calls to
DoSomethingNaug hty within DoSomething you would not be certain which call
threw the original exception...
Interesting I never specifically noticed that throw by itself modified the
entry for the routine it was in...
I'm curious what VB.NET's When clause does to the stack trace now...
Thanks for the info
Jay
"GoogleNewsRead erMan" <lo***********@ hotmail.com> wrote in message
news:11******** **************@ l41g2000cwc.goo glegroups.com.. . One arguable flaw here, though, is that you lose the location within the DoSomething() method --- the line number is that of the throw. So, in fact, the stack trace is modified, and not that of the original exception.
Jon wrote: <"Alvin Bruney [MVP]" <vapor at steaming post office>> wrote: > the rethrow does not preserve the stack. it is not a bug. You need preserve > the stack and then throw a new exception with the amended stack
Not true. Try this:
using System;
class Test { static void Main() { try { DoSomething(); } catch (Exception e) { Console.WriteLi ne (e); } }
static void DoSomething() { try { DoSomethingNaug hty(); } catch (Exception) { throw; } }
static void DoSomethingNaug hty() { string x = null; bool y = x.Equals("hello "); } }
Compile it with debug turned on and run it. I get a stack trace of: System.NullRefe renceException: Object reference not set to an instance of an object. at Test.DoSomethin gNaughty() in c:\test\Test.cs :line 32 at Test.DoSomethin g() in c:\test\Test.cs :line 25 at Test.Main() in c:\test\Test.cs :line 9
-- Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
> GoogleNewsReade rMan, Isn't that a matter of perspective?
Yes, that's why I said it was "arguable." Technically, the stack trace
is correct because the last location was that of the throw. But it
isn't what I want in all circumstances.
Although I can see the flip side also, if there were multiple calls
to DoSomethingNaug hty within DoSomething you would not be certain which
call threw the original exception...
Precisely!
Jay B. Harlow [MVP - Outlook] wrote: GoogleNewsReade rMan, Isn't that a matter of perspective?
The stack trace below(above?) DoSomething is preserved.
The stack trace within DoSomething is the line that threw the
exception (The throw itself).
The stack trace above(below?) DoSomething is maintained.
Although I can see the flip side also, if there were multiple calls
to DoSomethingNaug hty within DoSomething you would not be certain which
call threw the original exception...
Interesting I never specifically noticed that throw by itself
modified the entry for the routine it was in...
I'm curious what VB.NET's When clause does to the stack trace now...
Thanks for the info Jay
"GoogleNewsRead erMan" <lo***********@ hotmail.com> wrote in message news:11******** **************@ l41g2000cwc.goo glegroups.com.. . One arguable flaw here, though, is that you lose the location
within the DoSomething() method --- the line number is that of the throw.
So, in fact, the stack trace is modified, and not that of the original exception.
Jon wrote: <"Alvin Bruney [MVP]" <vapor at steaming post office>> wrote: > the rethrow does not preserve the stack. it is not a bug. You
need preserve > the stack and then throw a new exception with the amended stack
Not true. Try this:
using System;
class Test { static void Main() { try { DoSomething(); } catch (Exception e) { Console.WriteLi ne (e); } }
static void DoSomething() { try { DoSomethingNaug hty(); } catch (Exception) { throw; } }
static void DoSomethingNaug hty() { string x = null; bool y = x.Equals("hello "); } }
Compile it with debug turned on and run it. I get a stack trace
of: System.NullRefe renceException: Object reference not set to an instance of an object. at Test.DoSomethin gNaughty() in c:\test\Test.cs :line 32 at Test.DoSomethin g() in c:\test\Test.cs :line 25 at Test.Main() in c:\test\Test.cs :line 9
-- Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Christian Schuhegger |
last post by:
hi,
we are working on a c++ project which has a lot of thow list
specifications which in the end causes a lot of problems. now i would
like to remove them from the project. because the project has a size of
300000+ lines of code it is not so easy to do all of that manually.
therefore my question: is there a tool that you run on your sources and
which just converts the throw declarations into a comment? a tool that
only comments the...
|
by: Pierre Rouleau |
last post by:
The std::exception class defined in the Standard C++ <exception> header
specifies that the constructors could throw any exception becuase they
do not have a throw() specification.
Why is that? Is this because there could be an exception thrown when
the code creates a std::exception? I would assume that is not the case.
However, if I want to create a new exception class, derived from
std::exception (say MyException) then how can I...
|
by: Rennie deGraaf |
last post by:
I want to use throw() specifiers on some of my methods while debugging,
to help me keep track of what is going on, but remove them in release
builds. My method of doing this is
#ifdef DEBUG
#define THROW(...) throw (__VA_ARGS__)
#else
#define THROW(...)
#endif
|
by: wij |
last post by:
Hi:
In recent pondering, I am begining suspecting the usefulness of
throw specification in the language(I know this opinion have long
existed), even for delete!. The primary reason is that
reimplementing such members or functions cannot throw any types
other than specified (what would be the justifiable reason?
Besides, stack unwinding may be initiated by NPTL implemented
cancellation with unspecified throw type and compiler may enable...
|
by: Nenad Dobrilovic |
last post by:
Hi,
I have function which is throwning exception, like this:
public class LoggedException : Exception
{
public void Throw()
{
throw this;
}
}
| |
by: Arjen |
last post by:
Hi,
I'm reading the enterprise library documentation and there I see the throw
statement.
try
{
// run code
}
catch(Exception ex)
{
|
by: Ranganath |
last post by:
Why is throw keyword considered as an operator?
|
by: gnuist006 |
last post by:
I have some code like this:
(if (test)
(exit)
(do something))
or
(if (test)
|
by: =?Utf-8?B?UHVyZSBIZWFydA==?= |
last post by:
Hi
for me the throw statment is very confusing and un clear, i mean for
structured error handeling if the eception is not handeled it will search for
handlers in the calling methods hirarchy so why we need to re throw the
exception to the calling method since it will be done autimatically by .NET ?
whats the use of throw then ?
thank you
|
by: jason.cipriani |
last post by:
Consider this program, which defines a template class who's template
parameter is the type of an exception that can be thrown by members of
the class:
=== BEGIN EXAMPLE ===
#include <iostream>
#include <string>
using namespace std;
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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();...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |