What is exception handling in C++?

Use throw to report an error and try ... catch to handle it.

#include <iostream>
#include <stdexcept>

int main() {
    try {
        throw std::runtime_error("something went wrong");
    } catch (const std::exception& e) {
        std::cout << "Caught: " << e.what() << '\n';
    }
}
Output

Caught: something went wrong

See C++ Exceptions.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.