C++. An example of creating a class hierarchy for exception handling




An example of creating a class hierarchy for exception handling

The example creates its own class hierarchy that handles various arithmetic exceptions. Only a few classes handling exceptions have been selected for demonstration. At your own discretion, you can expand the list of exceptions to be processed, for example, the logarithm from scratch, the tangent of 90 degrees, and so on.

In this example, you can learn how to create your own hierarchy of exception handling classes in C ++.


Contents


Search other websites:

1. Demo program text for Console Application

The example demonstrates exception handling:

  • division by zero;
  • negative index in the array.

 

#include <iostream>
#include <string>
using namespace std;

// Creating a hierarchy of classes that handle exceptions
// abstract base class, it is impossible to create a class object
class BaseException
{
protected:
  // explanation for exception - common to all derived classes
  string text;

public:
  // pure virtual function that displays exception text
  virtual string what() = 0;
};

// class that corresponds to arithmetic operations
class ArithmeticException :public BaseException
{
public:
  // class constructors
  // default constructor
  ArithmeticException()
  {
    text = "Error. Arithmetic Exception.";
  }

  // the designer with the specified text
  ArithmeticException(string _text) { text = _text; }

  // override virtual what() function - is necessary
  string what() { return text; }
};

// A variation of the ArithmeticException class - dividing by 0,
// Since the class is at the bottom of the hierarchy and nothing can be inherited from it,
// the class declaration contains the final specifier
class DivideByZero final :public ArithmeticException
{
public:
  // constructor that contains text describing this exception
  DivideByZero() :ArithmeticException()
  {
    text = "Divide by zero."; // default text
  }

  // a constructor that receives a string of text from the user
  DivideByZero(string _text) :ArithmeticException(_text)
  {    
  }

  string what()
  {
    return text;
  }
};

// a variation of the BaseException class
// negative index in the array
// this is also the final class
class NegativeIndex final :public BaseException
{
public:
  // constructor gets the default argument
  NegativeIndex(string _text = "Error. Negative Index.") { text = _text; }

  // definitely need to override the what() the function
  string what() { return text; }
};

// Demo function for class NegativeIndex
void DemoExceptions1()
{
  int a[10];
  int index;

  for (int i = 0; i < 10; i++)
    a[i] = i * i;
  cout << "Input index: " << endl;
  cin >> index;

  // checking the index for a negative value
  if (index < 0)
    throw NegativeIndex(); // throw an exception of type NegativeIndex
  cout << "a[" << index << "] = " << a[index] << endl;
}

// Demo function for the ArithmeticException class
void DemoExceptions2()
{
  int a, b, c;
  cout << "a = "; cin >> a;
  cout << "b = "; cin >> b;

  // divide by 0 is prohibited
  if (b == 0)
    throw DivideByZero("Divide by 0.");

  cout << "a / b = " << (double)a / b << endl;
}

void main()
{
  try
  {
    // call the function that throws an exception
    DemoExceptions1(); // negative index check
    DemoExceptions2(); // check for "Divide by 0"
    cout << "OK!" << endl;
  }
  catch (NegativeIndex e)
  {
    // handle the exception
    cout << e.what() << endl;
  }
  catch (DivideByZero e)
  {
    cout << e.what() << endl;
  }
}

 






2. Explanation for use of the program. Class hierarchy diagram

The example creates an abstract base class BaseException in which are declared:

  • a text string of type string, which is an explanation for the exception;
  • pure virtual function what() designed to display the string text on the screen.

From the BaseException class, the ArithmeticException class is designed to handle arithmetic exceptions. Two classes are inherited from the ArithmeticException class:

  • class DivideByZero – designed to handle the division by zero;
  • class NegativeIndex – designed to handle a negative index from an array.

The general scheme of the implemented classes is shown in the figure.

C++. Exceptions. Console Application. Hierarchy of classes

 

3. The ability to inherit from the exception class

The example declares its own base class BaseException. It is recommended that your own exception handling classes be inherited from the C++ library’s exception class. That is, it would be more correct to declare a BaseException class as follows:

// class BaseException inherited from class exception
class BaseException: public exception
{
protected:
  // explanation for exception - common to all derived classes
  string text;

public:
  // pure virtual function that displays exception text
  virtual string what() = 0;
};

 


Related topics