7.2) Pre-defined Exception classes in C++

C++ Standard Library provides several pre-defined exception classes that cover a range of common error scenarios. These classes are part of the <stdexcept> header and are derived from the std::exception base class. Each exception class provides a specific category of error.

Here are some of the most commonly used exception classes in C++:

std::exception


This is the base class for all exception classes in C++. It provides a what() function that returns a C-style string (const char*) describing the error. It’s often used as a base class for custom exception hierarchies.

std::bad_alloc


This exception is thrown when memory allocation using new fails due to insufficient memory. It’s derived from std::exception and typically indicates a memory allocation failure.

std::bad_cast


This exception is thrown when a dynamic_cast operation fails during runtime type identification. It’s derived from std::bad_cast and typically indicates a failure in casting polymorphic types.

std::logic_error

This is the base class for exceptions that represent errors in program logic. It includes subclasses such as:

  • std::invalid_argument: Thrown when an argument to a function is invalid.
  • std::domain_error: Thrown when a domain-specific error occurs.
  • std::length_error: Thrown when a length-related error occurs

std::runtime_error


This is the base class for exceptions that represent errors during program execution. It includes subclasses such as:

  • std::range_error: Thrown when an out-of-range error occurs.
  • std::overflow_error: Thrown when an arithmetic overflow error occurs.
  • std::underflow_error: Thrown when an arithmetic underflow error occurs.

Here’s an example that demonstrates some of these exception classes:

#include <iostream>
#include <stdexcept>

int divide(int numerator, int denominator) {
    if (denominator == 0) {
        throw std::invalid_argument("Division by zero is not allowed");
    }
    return numerator / denominator;
}

int main() {
    try {
        int result = divide(10, 0);
        std::cout << "Result: " << result << std::endl;
    } catch (const std::exception& e) {
        std::cout << "Exception caught: " << e.what() << std::endl;
    }

    return 0;
}

In this example:

  • The divide function checks for a division by zero condition and throws an std::invalid_argument exception if it occurs.
  • In the main function, we call divide with an argument of 0 as the denominator, which leads to an exception being thrown.
  • The exception is caught using a catch block that catches std::exception and its subclasses. The error message from the exception is then printed using the what() function.

Output:

Exception caught: Division by zero is not allowed

These built-in exception classes help you to handle various types of errors more efficiently and to provide meaningful information to users or developers when exceptions occur. They offer a standardized way of categorizing and communicating errors in your code.

Leave a Reply