1 #define_crt_secure_no_warnings2#include <iostream>3 4 using namespacestd;5 6 classMyArray7 {8 Public:9MyArray (intlen);Ten~MyArray (); One A Public: - int&operator[](intindex); - intGetlen (); the - Public: - classesize - { + Public: -Esize (intlen) + { ASize =Len; at } - Virtual voidPrintferr () - { -cout <<"Size:"<< size <<Endl; - } - protected: in intsize; - Private: to }; + - classEnegative: Publicesize the { * Public: $Enegative (intsize): esize (size)Panax Notoginseng { - ; the } + Virtual voidPrintferr () A { thecout <<"enegative Exception Size:"<< size <<Endl; + } - protected: $ Private: $ }; - classEzero: Publicesize - { the Public: -Ezero (intsize): esize (size)Wuyi { the ; - } Wu Virtual voidPrintferr () - { Aboutcout <<"Ezero Exception Size:"<< size <<Endl; $ } - protected: - Private: - }; A classEtoobig: Publicesize + { the Public: -Etoobig (intsize): esize (size) $ { the ; the } the Virtual voidPrintferr () the { -cout <<"Etoobig Exception Size:"<< size <<Endl; in } the protected: the Private: About }; the classEtoosmall: Publicesize the { the Public: +Etoosmall (intsize): esize (size) - { the ;Bayi } the Virtual voidPrintferr () the { -cout <<"Etoosmall Exception Size:"<< size <<Endl; - } the protected: the Private: the }; the - the protected: the Private: the int*M_space;94 intM_len; the }; the the 98 AboutMyarray::myarray (intlen) - {101 if(Len <0)102 {103 Throwenegative (len);104 } the Else if(len = =0)106 {107 ThrowEzero (len);108 }109 Else if(Len > +) the {111 ThrowEtoobig (len); the }113 Else if(Len <3) the { the ThrowEtoosmall (len); the }117 118M_len =Len;119M_space =New int[Len]; - }121 122myarray::~MyArray ()123 {124 if(M_space! =NULL) the {126 Delete[]m_space;127M_space =NULL; -M_len =0;129 } the 131 } the int& MyArray::operator[](intindex)133 {134 returnM_space[index];135 }136 intMyarray::getlen ()137 {138 returnM_len;139 } $ 141 intMainvoid)142 {143 Try144 {145MyArray A (1);146 for(intI=0; I<a.getlen (); i++)147 {148A[i] = i+1;149printf"%d", A[i]); Max }151 } the Catch(myarray::esize&e)153 {154 E.printferr ();155 }156 157 Catch (...)158 {159 ; the }161 162 163 164cout <<"Hello world!"<<Endl;165System"Pause");166 return 0;167}
C + + Exception hierarchy