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.
Theoretically, exception handling is built on three keywords that form a defensive perimeter around your code:
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."
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.
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).
The most profound theoretical aspect of C++ exceptions is a process called Stack Unwinding.
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.
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.
C++ provides a set of standard exceptions defined in the <stdexcept> header:
| Exception | Meaning |
| runtime_error | Errors that can only be detected while the program is running. |
| logic_error | Errors in the program's logic (e.g., invalid arguments). |
| bad_alloc | Thrown by new when memory allocation fails. |
| out_of_range | Thrown when accessing an index outside an array/vector. |
| bad_cast | Thrown when a dynamic cast fails. |
#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; }
Copyright ©2025. All Rights Reserved Emblab THE RAVE INNOVATION