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
how to make program c++


C Program for FUNCTION OF FIND THE FACTORIAL float factorial(float); void main() {           float i=0,c=0;           clrscr();           printf("ENTER THE DIGI

For the circuit shown in Figure, verify the p.d. across resistor R3. If the total resistance of the circuit is 100Ω, verify the current flowing by resistor R1. Find also the

The question description: You are required to make a C++ program to conduct timber component (beam and column) structural design according to EC5 (all the relevant design codes

Can you overload a function depend only on whether a parameter is a value or a reference? A: No. Passing by value and reference looks identical to the caller.

Given an integer n and a permutation of numbers 1, 2 ... , n-1, n write a program to print the permutation that lexicographically precedes the given input permutation. If the given

What are pre-processor directives? - Pre-processor directives are placed at the beginning of a C program. They begin with # symbol. - This is the place, where library files

Increment and Decrement Operators.   The operator for increment is '++' and decrement is '-'. These operators enhances or decrease the value of a variable on which they are ope

C language lets us do this in a structure definition by putting: bit length after the variable that is. struct packed_struct { unsigned int f1:1; unsigned int f2:1; unsigned