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?

Using functions create a program, In rPeANUt implement the "char getchar()"...

In rPeANUt implement the "char getchar()" and "void printstring(char *str)" functions. Using these functions implement the following: void main() {    while (1) {       ch

Write down the code for binary search tree in c++?, A: BinarySearchTree.h ...

A: BinarySearchTree.h ---------------------- #ifndef BINARY_SEARCH_TREE_H_ #define BINARY_SEARCH_TREE_H_ #include "dsexceptions.h" #include // For NULL // Binary

Minimum shelfs, Write a program that finds the minimum total number of shel...

Write a program that finds the minimum total number of shelves, including the initial one required for this loading process

Lowest cost entry, code for solving optimal solution for matrix

code for solving optimal solution for matrix

Encoding and decoding, program for decode the encoded numbering format into...

program for decode the encoded numbering format into message

Sorted linked list , Node *orderedInsert(Node *p, int newval); /* Allocates...

Node *orderedInsert(Node *p, int newval); /* Allocates a new Node with data value newval and inserts into the ordered list with first node pointer p in such a way that the data va

When should you use multiple inheritance, There are 3 acceptable answers: "...

There are 3 acceptable answers: "Never," "Rarely "and" When the problem domain cannot be accurately modelled any other way."

Wap to print series from 1 to 10 & find its square and cube, WAP TO PRINT S...

WAP TO PRINT SERIES FROM 1 TO 10 & FIND ITS SQUARE AND CUBE # include stdio.h> # include conio.h> # include math.h>   void main () { int a=1,sqr=0,cube=0;

Pebblemerchant, write a c++ program for pebble merchant

write a c++ program for pebble merchant

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