⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 polyexceptions.cpp

📁 24学时攻克C++光盘源代码 深入浅出 方便实用
💻 CPP
字号:
 // Listing 24.2 catching exceptions polymorphically
 #include <iostream>

 const int DefaultSize = 10;

 // define the exception classes
 class xBoundary {};

 class xSize
 {
 public:
     xSize(int size):itsSize(size) {}
     ~xSize(){}
     virtual int GetSize() { return itsSize; }
     virtual void PrintError()
     { std::cout << "Size error. Received: "
         << itsSize << std::endl; }
 protected:
     int itsSize;
 };

 class xTooBig : public xSize
 {
 public:
     xTooBig(int size):xSize(size){}
     virtual void PrintError()
     {
         std::cout << "Too big! Received: ";
         std::cout << xSize::itsSize << std::endl;
     }
 };

 class xTooSmall : public xSize
 {
 public:
     xTooSmall(int size):xSize(size){}
     virtual void PrintError()
     {
         std::cout << "Too small! Received: ";
         std::cout << xSize::itsSize << std::endl;
     }
 };

 class xZero  : public xTooSmall
 {
 public:
     xZero(int size):xTooSmall(size){}
     virtual void PrintError()
     {
         std::cout << "Zero!!. Received: ";
         std::cout << xSize::itsSize << std::endl;
     }
 };

 class xNegative : public xSize
 {
 public:
     xNegative(int size):xSize(size){}
     virtual void PrintError()
     {
         std::cout << "Negative! Received: ";
         std::cout << xSize::itsSize << std::endl;
     }
 };

 class Array
 {
 public:
     // constructors
     Array(int itsSize = DefaultSize);
     Array(const Array &rhs);
     ~Array() { delete [] pType;}

     // operators
     Array& operator=(const Array&);
     int& operator[](int offSet);
     const int& operator[](int offSet) const;

     // accessors
     int GetitsSize() const { return itsSize; }

     // friend function
     friend std::ostream& operator<< (std::ostream&, const Array&);


 private:
     int *pType;
     int  itsSize;
 };

 Array::Array(int size):
 itsSize(size)
 {
     if (size == 0)
         throw xZero(size);

     if (size < 0)
         throw xNegative(size);

     if (size < 10)
         throw xTooSmall(size);

     if (size > 30000)
         throw xTooBig(size);

     pType = new int[size];
     for (int i = 0; i<size; i++)
         pType[i] = 0;
 }

 int& Array::operator[] (int offset)
 {
     int size = GetitsSize();
     if (offset >= 0 && offset < size)
         return pType[offset];
     throw xBoundary();
     return pType[offset];   // included to keep MSC happy, Borland reports warning
 }

 const int& Array::operator[] (int offset) const
 {
     int size = GetitsSize();
     if (offset >= 0 && offset < size)
         return pType[offset];
     throw xBoundary();
     return pType[offset];   // included to keep MSC happy, Borland reports warning
 }

 int main()
 {
     try
     {
         int choice;
         std::cout << "Enter the array size: ";
         std::cin >> choice;
         Array intArray(choice);
         for (int j = 0; j< 100; j++)
         {
             intArray[j] = j;
             std::cout << "intArray[" << j << "] okay..."
                 << std::endl;
         }
     }
     catch (xBoundary)
     {
         std::cout << "Unable to process your input!\n";
     }
     catch (xSize& theException)
     {
         theException.PrintError();
     }
     catch (...)
     {
         std::cout << "Something went wrong,"
             << "but I've no idea what!" << std::endl;
     }
     std::cout << "Done.\n";
     return 0;
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -