#include<iostream> using namespace std; class MyArray { public: MyArray(int m_len); ~MyArray(); public: class eSize { public: eSize(int len) { m_len11 = len; } virtual void printErr() { ; } protected: int m_len11; }; class eNegative :public eSize { public: eNegative(int len):eSize(len) { m_len11 = len; } virtual void printErr() { ; } }; class eZero:public eSize { public: eZero(int len):eSize(len) { m_len11 = len; } virtual void printErr() { ; } }; class eTooBig :public eSize { public: eTooBig(int len) :eSize(len) { m_len11 = len; } virtual void printErr() { ; } }; class eTooSmall :public eSize { public: eTooSmall(int len) :eSize(len) { m_len11 = len; } virtual void printErr() { ; } }; protected: private: int m_len; int *MySpace; }; MyArray::MyArray(int m_len) { this->m_len = m_len; if (m_len == 0) throw eZero(m_len); if (m_len <= 0) throw eNegative(m_len); if (m_len >= 1000) throw eTooBig(m_len); if (m_len <= 10) throw eTooSmall(m_len); } MyArray:: ~MyArray() { if (MySpace != NULL) { delete [] MySpace; MySpace = NULL; m_len = 0; } } int main() { try { MyArray a(-5); } catch (MyArray::eSize &e) { //cout << "len的大小: " << e.eSize(); e.printErr(); } catch (...) { } cout << "hello..." << endl; system("pause"); return 0; }
时间: 2024-10-31 01:36:58