zoukankan      html  css  js  c++  java
  • 多态和异常处理的完美结合

    #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;
    }
    

      

  • 相关阅读:
    线程循环的故事
    代码质量
    代码质量控制之异常控制
    面对象静态结构描述方法
    解决maven下载依赖包,pom文件错误问题
    Spring学习笔记
    java编程命名规范
    powershell使用
    vert.x中future的简单使用
    idea调整import包的顺序
  • 原文地址:https://www.cnblogs.com/xiaochige/p/6822312.html
Copyright © 2011-2022 走看看