C++

Exception Handling in C++

Exception Handling is a mechanism in C++ that allows a program to respond to unexpected events (runtime errors) such as division by zero, running out of memory, or failing to open a file. Instead of allowing the program to crash abruptly, exception handling provides a way to transfer control from the point where the error occurred to a block of code designed to fix or log the issue.

1. The Theory: The "Three-Key" System

Theoretically, exception handling is built on three keywords that form a defensive perimeter around your code:

  1. try: This block contains the "risky" code that might throw an error. You are telling the compiler, "Try to run this, but be ready for trouble."

  2. throw: When a problem is detected, the program "throws" an exception. This is like a runner passing a baton; the normal flow of the program stops immediately.

  3. catch: This block "catches" the thrown baton. It contains the logic to handle the error (e.g., printing an error message or resetting a variable).

2. Long Theory: Stack Unwinding and RAII

The most profound theoretical aspect of C++ exceptions is a process called Stack Unwinding.

A. The Unwinding Process

When an exception is thrown, the C++ runtime starts looking backward through the "call stack" for a matching catch block.

  • As it moves back through each function, it destroys all local objects in those functions in the reverse order of their creation.

  • The Benefit: Because of the RAII (Resource Acquisition Is Initialization) principle we discussed in Constructors, the destructors for these objects will run automatically. This ensures that memory is freed and files are closed even if the program is jumping across multiple functions to find a handler.

B. Types of Exceptions

While you can throw anything (an int, a string, etc.), the standard practice is to throw objects derived from the std::exception class. This provides a consistent interface, specifically the .what() method, which returns a description of the error.

3. The std::exception Hierarchy

C++ provides a set of standard exceptions defined in the <stdexcept> header:

ExceptionMeaning
runtime_errorErrors that can only be detected while the program is running.
logic_errorErrors in the program's logic (e.g., invalid arguments).
bad_allocThrown by new when memory allocation fails.
out_of_rangeThrown when accessing an index outside an array/vector.
bad_castThrown when a dynamic cast fails.

4. Practical Code Example

#include <iostream>
#include <stdexcept> // For standard exception classes
using namespace std;

double divide(double a, double b) {
    if (b == 0) {
        // THROW: Passing the error baton
        throw runtime_error("Mathematical Error: Division by zero!");
    }
    return a / b;
}

int main() {
    double x, y;
    cout << "Enter two numbers to divide: ";
    cin >> x >> y;

    try {
        // TRY: Attempting risky code
        double result = divide(x, y);
        cout << "Result: " << result << endl;
    } 
    catch (const runtime_error& e) {
        // CATCH: Handling the specific error
        cerr << "Caught an error: " << e.what() << endl;
    } 
    catch (...) {
        // ELLIPSIS: Catch-all for any other type of exception
        cerr << "An unknown error occurred." << endl;
    }

    cout << "Program continues normally..." << endl;
    return 0;
}
Upcoming Course
Upcoming Course
Learn More
Instructor Tips
Instructor Tips
View Tips
Join Community
Join Community
Join Now