Class exception. Using class to handle exceptions
Contents
- 1. Class exception. General information
- 2. An example of generating a simple exception of the exception class
- 3. An example of developing a function that uses the exception class to generate an exception can occur if the quadratic equation has no solution
- Related topics
Search other resources:
1. Class exception. General information
The C++ Standard Library provides the exception class for handling exceptions. To use the features of the exception class, you need to include the header <exception>
#include <exception>
Сlass has the following description:
class exception { public: exception() throw(); exception(const bad_exception& ob) throw(); virtual ~exception() throw(); exception &operator=(const exception& ob) throw(); virtual const char* what() const throw(); };
The exception class is the base class for all exceptions defined by the C++ standard. Any exceptional situation is described by a certain class. The following is a list of important classes inherited from the exception class:
- class bad_alloc – generates an exception in case of an unsuccessful attempt to allocate memory using the new operator;
- class bad_typeid – generates an exception when the typeid operator fails;
- class bad_cast – generates an exception in case of unsuccessful dynamic type casting.
Each of the inherited classes has a what() function that returns a pointer to a string terminated by ‘\0’. It is through this function that you can get a string with an explanation of the reason for the occurrence of a particular exceptional situation.
⇑
2. An example of generating a simple exception of the exception class
The example demonstrates artificially generating and catching an exception of type exception. According to a similar pattern, you can develop classes, functions that describe (implement) their own unique system of exceptions.
#include <iostream> #include <exception> using namespace std; // Класс exception, пример генерирования простого исключения void DemoException() { try { std::exception err("My error"); // create an object throw err; // generate the exception } catch (std::exception& error) { std::cout << error.what() << endl; } } void main(void) { DemoException(); return; }
Program result
My error
⇑
3. An example of developing a function that uses the exception class to generate an exception can occur if the quadratic equation has no solution
A function is proposed that solves a quadratic equation based on the input coefficients a, b, c. As you know, for some values of a, b, c it is impossible to obtain a solution to the equation. For demonstration purposes, the function throws an exception if the quadratic equation has no roots. The exception class is used to create an exception.
#include <iostream> #include <exception> // needed to use the exception class using namespace std; // An example of catching the exception of the absence of a solution // to a quadratic equation in a function. // Parameters: // - a, b, c - equation coefficients a*x^2 + b*x + c = 0; // - x1, x2 - solution of the equation. // The function returns true if a solution exists, // otherwise, the function returns false. bool DemoSquareEquation(double a, double b, double c, double& x1, double& x2) { // 1. Additional internal variables double D; // discriminant // 2. Calculate discriminant D = b * b - 4 * a * c; // 3. Processing the D value try { if (D < 0) // If the equation has no solution { // Then throw the exception // 3.1. Create an error object of type exception exception error("The equation has no roots"); // 3.2. Throw the exception throw error; } } catch (exception& e) { // 3.3. Print the result cout << e.what() << endl; return false; } // 4. If the equation has a solution, then get and return the result x1 = (-b - sqrt(D)) / (2 * a); x2 = (-b + sqrt(D)) / (2 * a); // 5. Return the result return true; } void main(void) { // 1. Declare internal variables double x1, x2; // 2. Invoke the DemoSquareEquation() if (DemoSquareEquation(2, 3, 4, x1, x2)) { cout << "x1 = " << x1 << endl; cout << "x2 = " << x2 << endl; } return; }
⇑
Related topics
- The concept of exception. Instruction try-catch. Examples. Operator throw
- An example of creating a class hierarchy for exception handling
⇑