How do one throw polymorphically?, C/C++ Programming

How do one throw polymorphically?              

A: Sometimes people write code such as:

class MyExceptionBase { };

class MyExceptionDerived : public MyExceptionBase { };

void f(MyExceptionBase& e)

{

// ... throw e;

}

void g()

{

MyExceptionDerived e;

try {

f(e);

}

catch (MyExceptionDerived& e) {

...code to handle MyExceptionDerived...

}

catch (...) {

...code to handle other exceptions...

}

}

If you attempt this, you might be astounded at run-time while your catch (...) clause is entered, & not your catch (MyExceptionDerived&) clause. It happens because you didn't throw polymorphically. The statement throw e , in function f(); throws an object  along with the same type as the static type of the expression e. In other terms, it throws an example of MyExceptionBase. The throw statement acts as-if the thrown object is copied, as opposed to developing a "virtual copy".

Luckily it's comparatively easy to correct:

class MyExceptionBase {

public:

virtual void raise();

};

void MyExceptionBase::raise()

{ throw *this; }

class MyExceptionDerived : public MyExceptionBase {

public:

virtual void raise();

};

 

void MyExceptionDerived::raise()

{ throw *this; }

void f(MyExceptionBase& e)

{

// ... e.raise();

}

void g()

{

MyExceptionDerived e;

try {

f(e);

}

catch (MyExceptionDerived& e) {

...code to handle MyExceptionDerived...

}

catch (...) {

...code to handle other exceptions...

}

}

Note down that the throw statement has been moved in a virtual function. The statement e.raise() will show polymorphic behavior, as raise() is declared virtual & e was passed through reference. As before, the thrown object will be of static type of the argument in the throw statement, however in MyExceptionDerived::raise(), that static type is MyExceptionDerived not MyExceptionBase.

 

Posted Date: 3/19/2013 8:52:07 AM | Location : United States







Related Discussions:- How do one throw polymorphically?, Assignment Help, Ask Question on How do one throw polymorphically?, Get Answer, Expert's Help, How do one throw polymorphically? Discussions

Write discussion on How do one throw polymorphically?
Your posts are moderated
Related Questions
#A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrom

One person who is specialist at programming and solving problems with a computer Project Description: Potential computer, hardware, programming and software genius, I look

Sorted directory - C++ program: Write a program in c to sorting a directory. int main( int argc, char *argv[] ) {     if( argc 3 )         {         cerr

Program to Open a file in C++: int main() {                  char ch;                  ifstream f1 ("OLDFILE.TXT");                  ofstream f2 ("NEWFILE.TXT");

Hey vrcoder I have got similar way but the game builder has sent me the game unfinished and left. I saw it on a link on line but they have wiped the old message with the link in it


write a c program to solve exanple of lamis therom


What do you mean by a sequential access file? - When writing programs which store and retrieve data in a file, it's possible to designate that file into various forms. - A s

Virtual Functions The keyword virtual was previously used to resolve ambiguity for a class derived from two classes, both having a common ancestor. These classes are known as v