473,320 Members | 1,939 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,320 software developers and data experts.

Any idea why do this program crash when I throw derived object as shown ?

#include <iostream>
#include <map>

using namespace std;

class Base {
public:
~Base() {}
virtual void example() {
cout <<"Base Example"<<endl;
}
};

class Derived {
public:
~Derived() {}
void example() {
cout <<"Derived Example"<<endl;
}
};

void main()
{
// Outer try block.
try {
//Inner try block.
try {
Derived d;

throw d; // ||| C...R....A....S....H

}
//Catch by reference -- won't slice.
catch (Base& ex) {

ex.example(); //O.K.

//Rethrow ...

throw ex; //Mistake -- slices!
//Should just use "throw;".

}
//END Inner try block.
}

// Should be fine, but ...
catch(Base& ex) {

ex.example(); //... not what we expected!
}
}
Jul 22 '05 #1
5 1245
ksharma wrote:
#include <iostream>
#include <map>

using namespace std;

class Base {
public:
~Base() {}
virtual void example() {
cout <<"Base Example"<<endl;
}
};

class Derived {
public:
~Derived() {}
void example() {
cout <<"Derived Example"<<endl;
}
};

void main()
main must return int.
{
// Outer try block.
try {
//Inner try block.
try {
Derived d;

throw d; // ||| C...R....A....S....H
I see no reason for a crash here.

}
//Catch by reference -- won't slice.
No it wont, neither would it if you caught by value, since Base and
Derived are unrelated classes. I guess you actually wanted to derive
Derived from Base.
catch (Base& ex) {

ex.example(); //O.K.

//Rethrow ...

throw ex; //Mistake -- slices!
//Should just use "throw;".

}
//END Inner try block.
}

// Should be fine, but ...
catch(Base& ex) {

ex.example(); //... not what we expected!
}
}


What do you mean "not what we expected"? How do you know what the result
was if your code crashed before reaching this part?

Jul 22 '05 #2
ks*****@cs.jhu.edu (ksharma) wrote in
news:b7*************************@posting.google.co m:
#include <iostream>
#include <map>

using namespace std;

class Base {
public:
~Base() {}
virtual void example() {
cout <<"Base Example"<<endl;
}
};

class Derived {
you did actually mean:

class Derived : public Base {
public:
~Derived() {}
void example() {
cout <<"Derived Example"<<endl;
}
};

void main()
{
// Outer try block.
try {
//Inner try block.
try {
Derived d;

throw d; // ||| C...R....A....S....H

}
//Catch by reference -- won't slice.
catch (Base& ex) {

ex.example(); //O.K.

//Rethrow ...

throw ex; //Mistake -- slices!
//Should just use "throw;".

}
//END Inner try block.
}

// Should be fine, but ...
catch(Base& ex) {

ex.example(); //... not what we expected!
}
}


the following works as expected:

#include <iostream>

using namespace std;

class Base {
public:
virtual ~Base() {}
virtual void example() const {
cout <<"Base Example"<<endl;
}
};

class Derived : public Base {
public:
~Derived() {}
void example() const {
cout <<"Derived Example"<<endl;
}
};

int main() {
try {
try {
Derived d;
throw d;
}
catch (Base const& ex) {
ex.example();
throw ex;
}
}
catch(Base const& ex) {
ex.example();
}
}

--
:: bartekd [at] o2 [dot] pl

Jul 22 '05 #3
* ks*****@cs.jhu.edu (ksharma) schriebt:
#include <iostream>
#include <map>

using namespace std;

class Base {
public:
~Base() {}
virtual void example() {
cout <<"Base Example"<<endl;
}
};

class Derived {
What is this class mean to be derived from?

public:
~Derived() {}
void example() {
cout <<"Derived Example"<<endl;
}
};

void main()
void return type is invalid for main, so you have
undefined behavior.
{
// Outer try block.
try {
//Inner try block.
try {
Derived d;

throw d; // ||| C...R....A....S....H

}
//Catch by reference -- won't slice.
catch (Base& ex) {
Never catch by reference to non-const.


ex.example(); //O.K.

//Rethrow ...

throw ex; //Mistake -- slices!
//Should just use "throw;".

}
//END Inner try block.
} // Should be fine, but ...
catch(Base& ex) {

ex.example(); //... not what we expected!
What did you expect, and what did you get?
}
}


--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #4
bartek wrote:
the following works as expected:

#include <iostream>

using namespace std;

class Base {
public:
virtual ~Base() {}
--- snip ---


The OP didn't make the base destructor virtual, even though
he has polymorphic methods. You did that, and it works.
This of course could be a mere coincidence.

- J.
Jul 22 '05 #5
al***@start.no (Alf P. Steinbach) wrote in message news:<40*****************@news.individual.net>...


{
// Outer try block.
try {
//Inner try block.
try {
Derived d;

throw d; // ||| C...R....A....S....H

}
//Catch by reference -- won't slice.
catch (Base& ex) {


Never catch by reference to non-const.


Can you explain the reason behind it? Thanks!
Jul 22 '05 #6

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

Similar topics

7
by: relient | last post by:
Question: Why can't you access a private inherited field from a base class in a derived class? I have a *theory* of how this works, of which, I'm not completely sure of but makes logical sense to...
5
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...
12
by: Chris M. Thomasson | last post by:
Keep in mind that I am a C programmer; well, anyway here is the C++ program... ______________________________________________________________________ #include <cstdio> #include <cstdlib>...
0
by: Chris M. Thomasson | last post by:
"Chris M. Thomasson" <no@spam.invalidwrote in message news:...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.