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

K

ksharma

#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!
}
}
 
R

Rolf Magnus

ksharma said:
#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?
 
B

bartek

(e-mail address removed) (ksharma) wrote in
#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();
}
}
 
A

Alf P. Steinbach

* (e-mail address removed) (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?
 
J

Jacek Dziedzic

bartek said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top