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

Assignment Help:

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.

 


Related Discussions:- How do one throw polymorphically?

Write a program to add co-ordinates of the plane, Write a program to add co...

Write a program to add co-ordinates of the plane The class having x and y co-ordinates. Create three objects. Use a constructor to pass one pair of co-ordinates and a funct

Algorithm and flowcharts, algorithm to find out all the factors of given po...

algorithm to find out all the factors of given positive integers

Subrotine assembly language, You have to write a subroutine (assembly langu...

You have to write a subroutine (assembly language code using NASM) for the following equation.

C Programming, Develop a function to calculate sum of n even integers start...

Develop a function to calculate sum of n even integers starting from a given even integer

.testvita, answer for tcs is working question

answer for tcs is working question

When is a template a better solution than a base class, When you are design...

When you are designing a generic class to have or otherwise manage objects of other types, when the format and behavior of those other types are unimportant to their containment or

Memory management by c program, Memory management by c program: Write ...

Memory management by c program: Write a program to memory management in c program unsigned max( unsigned, unsigned );   int BaseMemBlocks::allocBlock( size_t sz )

Add words in dictionary - c++ program, Add words in Dictionary: void D...

Add words in Dictionary: void Dictionary::add( Object& objectToAdd ) {     if( !objectToAdd.isAssociation() )         ClassLib_error( __ENOTASSOC );     else

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd