多态和异常处理的完美结合
Posted 菜鸟根据地
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多态和异常处理的完美结合相关的知识,希望对你有一定的参考价值。
#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; }
以上是关于多态和异常处理的完美结合的主要内容,如果未能解决你的问题,请参考以下文章