473,412 Members | 4,966 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,412 software developers and data experts.

rethrowing exceptions: why does this happen?

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 raw std::exception?

#include <stdexcept>
#include <iostream>
void generate(){
throw std::invalid_argument("generate() invalid argument exception");
}

void handle(bool broken) {
try {
generate();
} catch (std::exception& e) {
std::cout<< "handle:"<< e.what() <<std::endl;
if(broken)throw e;
throw;
}
}

void rehandle(bool broken=false) {
try {
handle(broken);
} catch (std::exception& e) {
std::cout<< "rehandle:"<< e.what() <<std::endl;
if(broken)throw e;
throw;
}
}
int main() {
try {
rehandle();
} catch(std::exception& e) {
std::cout<< "main:"<< e.what() <<std::endl;
}

try {
rehandle(true);
} catch(std::exception& e) {
std::cout<< "main:"<< e.what() <<std::endl;
}
}

/*-----------output---------
handle:generate() invalid argument exception
rehandle:generate() invalid argument exception
main:generate() invalid argument exception
handle:generate() invalid argument exception
rehandle:St9exception
main:St9exception
*/
--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Nov 17 '06 #1
5 2833
Steven T. Hatton wrote:
>
In terms of what goes on at runtime, why does the `throw e' act as if I
threw a raw std::exception?
A throw-expression throws creates a temporary object whose type is the
static type of its argument. Since e is a reference to std::exception,
the type of the temporary object is std::exception.

Forget for the moment that e is a reference to the exception object.
Pretend that it's just some reference to an object of type
std::exception. In that case, the only legitimate copy you could make of
it would be an object of type std::exception, because you don't know
it's dynamic type.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Nov 17 '06 #2
Steven T. Hatton wrote:
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 raw std::exception?
Becuase "throw e" makes a copy of e and since the type is
"std::exception", you get a slice of the "invalid_argument" exception
and loose any information about the exception. "throw" however throws
the same object that was caught.
Nov 17 '06 #3

Steven T. Hatton wrote:
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 raw std::exception?

#include <stdexcept>
#include <iostream>
void generate(){
throw std::invalid_argument("generate() invalid argument exception");
}

void handle(bool broken) {
try {
generate();
} catch (std::exception& e) {
std::cout<< "handle:"<< e.what() <<std::endl;
if(broken)throw e;
throw;
}
}

void rehandle(bool broken=false) {
try {
handle(broken);
} catch (std::exception& e) {
std::cout<< "rehandle:"<< e.what() <<std::endl;
if(broken)throw e;
throw;
}
}
int main() {
try {
rehandle();
} catch(std::exception& e) {
std::cout<< "main:"<< e.what() <<std::endl;
}

try {
rehandle(true);
} catch(std::exception& e) {
std::cout<< "main:"<< e.what() <<std::endl;
}
}

/*-----------output---------
handle:generate() invalid argument exception
rehandle:generate() invalid argument exception
main:generate() invalid argument exception
handle:generate() invalid argument exception
rehandle:St9exception
main:St9exception
*/
--
You are rethrowing a copied slice of the original object.

#include <iostream>
#include <typeinfo>

struct Base
{
virtual ~Base() { }
};

struct Derived : public Base
{
};

void foo()
{
try {
throw Derived();
}
catch(const Base& r_b)
{
std::cerr << typeid(r_b).name();
std::cerr << std::endl;
// throw;
throw r_b; // copy a slice
}
}

int main()
{
try {
foo();
}
catch(const Base& r_b)
{
std::cerr << typeid(r_b).name();
std::cerr << std::endl;
}
}

/*
Derived
Base
*/

Nov 17 '06 #4
Salt_Peter wrote:
>
Steven T. Hatton wrote:
>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 raw std::exception?

#include <stdexcept>
#include <iostream>
void generate(){
throw std::invalid_argument("generate() invalid argument exception");
}

void handle(bool broken) {
try {
generate();
} catch (std::exception& e) {
std::cout<< "handle:"<< e.what() <<std::endl;
if(broken)throw e;
throw;
}
}

void rehandle(bool broken=false) {
try {
handle(broken);
} catch (std::exception& e) {
std::cout<< "rehandle:"<< e.what() <<std::endl;
if(broken)throw e;
throw;
}
}
int main() {
try {
rehandle();
} catch(std::exception& e) {
std::cout<< "main:"<< e.what() <<std::endl;
}

try {
rehandle(true);
} catch(std::exception& e) {
std::cout<< "main:"<< e.what() <<std::endl;
}
}

/*-----------output---------
handle:generate() invalid argument exception
rehandle:generate() invalid argument exception
main:generate() invalid argument exception
handle:generate() invalid argument exception
rehandle:St9exception
main:St9exception
*/
--

You are rethrowing a copied slice of the original object.

#include <iostream>
#include <typeinfo>

struct Base
{
virtual ~Base() { }
};

struct Derived : public Base
{
};

void foo()
{
try {
throw Derived();
}
catch(const Base& r_b)
{
std::cerr << typeid(r_b).name();
std::cerr << std::endl;
// throw;
throw r_b; // copy a slice
}
}

int main()
{
try {
foo();
}
catch(const Base& r_b)
{
std::cerr << typeid(r_b).name();
std::cerr << std::endl;
}
}

/*
Derived
Base
*/
I believe Pete Becker had it right. Even if I pass the same time as the
original event, it comes out empty. What happens is that the compiler
borrows the constructor from the class used as the catch parameter, and
constructs a new instance.
--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Nov 20 '06 #5

Steven T. Hatton wrote:
Salt_Peter wrote:

Steven T. Hatton wrote:
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.
re>
In terms of what goes on at runtime, why does the `throw e' act as if I
threw a raw std::exception?

#include <stdexcept>
#include <iostream>
void generate(){
throw std::invalid_argument("generate() invalid argument exception");
}

void handle(bool broken) {
try {
generate();
} catch (std::exception& e) {
std::cout<< "handle:"<< e.what() <<std::endl;
if(broken)throw e;
throw;
}
}

void rehandle(bool broken=false) {
try {
handle(broken);
} catch (std::exception& e) {
std::cout<< "rehandle:"<< e.what() <<std::endl;
if(broken)throw e;
throw;
}
}
int main() {
try {
rehandle();
} catch(std::exception& e) {
std::cout<< "main:"<< e.what() <<std::endl;
}

try {
rehandle(true);
} catch(std::exception& e) {
std::cout<< "main:"<< e.what() <<std::endl;
}
}

/*-----------output---------
handle:generate() invalid argument exception
rehandle:generate() invalid argument exception
main:generate() invalid argument exception
handle:generate() invalid argument exception
rehandle:St9exception
main:St9exception
*/
--
You are rethrowing a copied slice of the original object.

#include <iostream>
#include <typeinfo>

struct Base
{
virtual ~Base() { }
};

struct Derived : public Base
{
};

void foo()
{
try {
throw Derived();
}
catch(const Base& r_b)
{
std::cerr << typeid(r_b).name();
std::cerr << std::endl;
// throw;
throw r_b; // copy a slice
}
}

int main()
{
try {
foo();
}
catch(const Base& r_b)
{
std::cerr << typeid(r_b).name();
std::cerr << std::endl;
}
}

/*
Derived
Base
*/

I believe Pete Becker had it right. Even if I pass the same time as the
original event, it comes out empty. What happens is that the compiler
borrows the constructor from the class used as the catch parameter, and
constructs a new instance.
In the case where you throw a base or attempt reconstruction of the
derived object, yes.
In the case you *rethrow* the derived exception, however, the catch
block in main captures the original derived object via a pointer to its
base.
output:
/*
Derived
Derived
*/
To "pass" or rethrow the original "event":
throw; // rethrow the exception

--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Nov 20 '06 #6

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

Similar topics

33
by: Steven Bethard | last post by:
I feel like this has probably been answered before, but I couldn't find something quite like it in the archives. Feel free to point me somewhere if you know where this has already been answered. ...
26
by: OvErboRed | last post by:
I just read a whole bunch of threads on microsoft.public.dotnet.* regarding checked exceptions (the longest-running of which seems to be <cJQQ9.4419 $j94.834878@news02.tsnz.net>. My personal...
2
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...
822
by: Turamnvia Suouriviaskimatta | last post by:
I 'm following various posting in "comp.lang.ada, comp.lang.c++ , comp.realtime, comp.software-eng" groups regarding selection of a programming language of C, C++ or Ada for safety critical...
2
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;
4
by: Steve | last post by:
I have read a couple articles online, read my Jesse Liberty book but I am still confused as to just what the best practices are for using exceptions. I keep changing how I'm working with them and...
15
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
4
by: Coder Guy | last post by:
I am implementing an asynchronous handler (similar to Control.EndInvoke) and if the other thread throws an exception, I catch it and rethrow it on the current thread. How can I rethrow the...
13
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
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
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...
0
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,...
0
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...

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.