Saturday 15 September 2012

c++ - Cant throw base class exception from dervied class -


I have written a code in which instead of throwing exceptions to the class class, I threw the base class exception from my derived class

  #include & lt; Iostream & gt; using namespace std; Class Base {Public: Class Base Exposure {}; Class Derived Exceptions: Public Base Exposure {}; Virtual Zero (DerivedException) {DerivedException (throw); } Virtual Zero (throw) (Base Exestation) {Base Expansion (); }}; Category created: Public base {Public: zero F (throw) (baseexception) {BaseExation (throw); } Virtual Zero (throw) (Base Exposure) {DerivedException Throw (); }}; Int main () {Derived D; Return 0; }   

It fails to compile and says

  line 18: error: throwing defeats for virtual zero The specifier :: F (throw) (base :: baseexception) due to the end of the compilation - waffle-errors   

I searched the net and it seems that the derivative class violated that contract Does what is done in the base class.

My suspicion is that the base class has created a contract for a derived class object, however, I am using the base class, so the contract is broken.

The problem is here:

  Virtual Zero Base: f   

and

  Zero extracted: F (Base Exception)   

You originally promised That anyBase.f a DerivedException (more specific) will throw exceptions. Then in your derived class you are trying to "rest" to make this promise, will tell you that you get more general BaseException when you get a class from another class You can not do so. Either replace the throw (baseexception) or the second throw (dedicated expansion) to the first. You can also do both, because Restricting and that contract made by derived class base Is allowed.

No comments:

Post a Comment