zoukankan      html  css  js  c++  java
  • 类开发中异常的结构设置

    #include <iostream>
    using namespace std;
    
    class MyArray
    {
    public:
        MyArray(int len);
        ~MyArray();
    public:
        int & operator[](int index);
        int getLen();
    
        class eSize
        {
        public:
            eSize(int size)
            {
                m_size = size;
            }
            virtual void printErr()
            {
                cout << "size:" << m_size << " ";
            }
        protected:
            int m_size;
        };
        class eNegative : public eSize
        {
        public:
            eNegative(int size) : eSize(size)
            {
                ;
            }
            virtual void printErr()
            {
                cout << "eNegative 类型 size:" << m_size << " ";
            }
        };
        class eZero : public eSize
        {
        public:
            eZero(int size) : eSize(size)
            {
                ;
            }
            virtual void printErr()
            {
                cout << "eZero 类型 size:" << m_size << " ";
            }
        };
        class eTooBig : public eSize
        {
        public:
            eTooBig(int size) : eSize(size)
            {
                ;
            }
            virtual void printErr()
            {
                cout << "eTooBig 类型 size:" << m_size << " ";
            }
        };
        class eTooSmall : public eSize
        {
        public:
            eTooSmall(int size) : eSize(size)
            {
                ;
            }
            virtual void printErr()
            {
                cout << "eTooSmall 类型 size:" << m_size << " ";
            }
        };
    
    private:
        int *m_space;
        int m_len;
    };
    
    
    MyArray::MyArray(int len)
    {
        if (len  < 0)
        {
            throw eNegative(len);
        }
        else if (len == 0)
        {
            throw eZero(len);
        }
        else if (len > 1000)
        {
            throw eTooBig(len);
        }
        else if (len < 3)
        {
            throw eTooSmall(len);
        }
        m_len = len;
        m_space = new int[len];
    }
    
    MyArray::~MyArray()
    {
        if (m_space != NULL)
        {
            delete [] m_space;
            m_space = NULL;
            m_len = 0;
        }
    }
    
    int & MyArray::operator[](int index)
    {
        return m_space[index];
    }
    
    int MyArray::getLen()
    {
        return m_len;
    }
    
    void main()
    {
    
        try
        {
            MyArray a(-5);
            for (int i=0; i<a.getLen(); i++)
            {
                a[i] = i+1;
                printf("%d ", a[i]);
            }
        }
        catch(MyArray::eSize &e)
        {
            //cout <<  "len的大小: " << e.eSize();
            e.printErr();
        }
        catch (...)
        {
        }
    
    
        cout<<"hello..."<<endl;
        system("pause");
        return ;
    }
    
    // 不推荐
    void main51()
    {
        
        try
        {
            MyArray a(-5);
            for (int i=0; i<a.getLen(); i++)
            {
                a[i] = i+1;
                printf("%d ", a[i]);
            }
        }
        catch(MyArray::eNegative e)
        {
            cout << "eNegative 类型异常" << endl;
        }
        catch(MyArray::eZero e)
        {
            cout << "eZero 类型异常" << endl;
        }
        catch(MyArray::eTooBig e)
        {
            cout << "eTooBig 类型异常" << endl;
        }
        catch(MyArray::eTooSmall e)
        {
            cout << "eTooSmall 类型异常" << endl;
        }
        
        catch (...)
        {
        }
        
    
        cout<<"hello..."<<endl;
        system("pause");
        return ;
    }
  • 相关阅读:
    ABAP 程序中的类 沧海
    ABAP类的方法(转载) 沧海
    More than 100 ABAP Interview Faq's(2) 沧海
    SAP and ABAP Memory总结 沧海
    ABAP Frequently Asked Question 沧海
    ABAP System Reports(Additional functions) 沧海
    ABAP Questions Commonly Asked 1 沧海
    ABAP Tips and Tricks 沧海
    ABAP System Fields 沧海
    ABAP 面试问题及答案(一):数据库更新及更改 SAP Standard (转) 沧海
  • 原文地址:https://www.cnblogs.com/lyjbk/p/13202800.html
Copyright © 2011-2022 走看看