C++ Tutorial/Exceptions/Custom Exception

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Custom exception class

<source lang="cpp">#include <iostream>

  1. include <string>

using namespace std; class Exception { public:

  Exception(const string& msg) : msg_(msg) {}
 ~Exception( ) {}
  string getMessage( ) const {return(msg_);}

private:

  string msg_;

}; void f( ) {

  throw(Exception("Mr. Sulu"));

} int main( ) {

  try {
     f( );
  }
  catch(Exception& e) {
     cout << "You threw an exception: " << e.getMessage( ) << endl;
  }

}</source>

You threw an exception: Mr. Sulu

Throw a custom exception object

<source lang="cpp">#include <iostream> using std::cout; using std::endl; class Trouble {

 public:
   Trouble(const char* pStr = "There"s a problem") : pMessage(pStr) {}
   const char* what() const {return pMessage;}
 private:
   const char* pMessage;

}; int main() {

 for(int i = 0 ; i < 2 ; i++) {
   try     {
     if(i == 0)
       throw Trouble();
     else
       throw Trouble("Nobody knows the trouble I"ve seen...");
   }
   catch(const Trouble& t) {
     cout << endl << "Exception: " << t.what();
   }
 }
 return 0;

}</source>

Exception: There"s a problem
Exception: Nobody knows the trouble I"ve seen..."

Throw your own exception class based on runtime_error

<source lang="cpp">#include <iostream>

  1. include <stdexcept>

using std::cin; using std::cout; using std::endl; using std::runtime_error; class DivideByZeroException : public runtime_error { public:

  DivideByZeroException::DivideByZeroException(): runtime_error( "attempted to
divide by zero" ) {}

}; double quotient( int numerator, int denominator ) {

  throw DivideByZeroException(); // terminate function
  return 0;

} int main() {

   try
   {
      double result = quotient( 1, 1 );
      cout << "The quotient is: " << result << endl;
   }
   catch ( DivideByZeroException &divideByZeroException )
   {
      cout << "Exception occurred: " << divideByZeroException.what() << endl;
   }
   return 0;

}</source>

Exception occurred: attempted to divide by zero

Use custom exception in your own Array class

<source lang="cpp">#include <iostream>

const int DefaultSize = 10;

// define the exception classes
class ArrayIndexOutofBoundException {};

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

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

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

class ArraySizeZeroException  : public ArraySizeTooSmallException
{
public:
    ArraySizeZeroException(int size):ArraySizeTooSmallException(size){}
    virtual void PrintError() 
    { 
        std::cout << "Zero!!. Received: ";
        std::cout << BaseArraySizeException::itsSize << std::endl;
    }
};

class ArraySizeNegativeException : public BaseArraySizeException
{
public:
    ArraySizeNegativeException(int size):BaseArraySizeException(size){}
    virtual void PrintError() 
    { 
        std::cout << "Negative! Received: ";
        std::cout << BaseArraySizeException::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 ArraySizeZeroException(size);

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

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

    if (size > 30000)
        throw ArraySizeTooBigException(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 < GetitsSize())
        return pType[offset];
    throw ArrayIndexOutofBoundException();
    return pType[offset];
}

const int& Array::operator[] (int offset) const
{
    int size = GetitsSize();
    if (offset >= 0 && offset < GetitsSize())
        return pType[offset];
    throw ArrayIndexOutofBoundException();
    return pType[offset];
}

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 (ArrayIndexOutofBoundException)
    {
        std::cout << "Unable to process your input!\n";
    }
    catch (BaseArraySizeException& theException)
    {
        theException.PrintError();
    }
    catch (...)
    {
        std::cout << "Something went wrong,"
            << "but I"ve no idea what!" << std::endl;
    }
    std::cout << "Done.\n";
    return 0;
}</source>
Enter the array size: 12
intArray[0] okay...
intArray[1] okay...
intArray[2] okay...
intArray[3] okay...
intArray[4] okay...
intArray[5] okay...
intArray[6] okay...
intArray[7] okay...
intArray[8] okay...
intArray[9] okay...
intArray[10] okay...
intArray[11] okay...
Unable to process your input!
Done.