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
Define Scope Rules of C program - Computer Programming? The fundamental rule of scope is that identifiers are accessible only within the block in which they are declared and th

A: No. There is nothing you can do in C++ which you cannot do in C. In spite of everything

New App Production company is seeking an organized and highly accomplished design team to prepare the first (and potentially more) of my companies' App concepts, intended completel


External Variables Different functions of the similar program can be written in different source files and can be compiled together. The scope of a global variable is not limit

Write a Program to illustrate Array with Strings? main() { static char name[]="devdas"; int i; i=0; while(name[i]!='\0') { printf("%c",name[i]); i=i+1; } } In the

Write a ‘C’ program to accept any 3 digit integer number from the keyboard and display the word equivalent representation of the given number.

c-program to find the area under the curve y=f(x) between x=a and x=b integrate y=f(x) between the limits of a and b   #include float start_point, /

The Main program is slightly more complicated and you need to think about the I/O process. First of all, for most applications you do not call the low level routines GetChar and P

Access privileges 1. If the designer of the base class needs no one, not even a derived class to access a member, then that member should be made private. 2. If the designer