C++ Reference ยท Keywords and Statements
try, catch and throw
Reference for C++ exception handling: throw, try and catch blocks, catch order, rethrowing, catch (...), function-try-blocks and stack unwinding.
Exceptions are C++'s mechanism for reporting errors that the current function cannot handle. throw expr; copies the value into a special exception object and starts stack unwinding: the runtime leaves scopes one by one, running the destructors of local objects, until it finds a catch clause whose parameter type matches. If none is found, std::terminate is called and the program ends.
Throw objects of class types derived from std::exception, catch them by reference, and let RAII objects release resources during unwinding. Exceptions are for exceptional, hard-to-handle conditions, not for ordinary control flow.
try { riskyWork(); } catch (const std::exception& e) { std::cerr << e.what() << '\n'; }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
throw expression; throw; // rethrow current exception try compound-statement handler-seq handler: catch (type-or-declaration) compound-statement catch (...) compound-statement return-type function(params) try { body } catch (type) { handler }
| Part | Description |
|---|---|
throw expression; |
Copies or moves the value into a new exception object (its type is the static, decayed type of the expression) and begins unwinding. |
throw; |
Inside a handler (or a function called from one), rethrows the exception being handled without copying it. With no active exception it calls std::terminate. |
catch (T e), catch (const T& e) |
Matches if the thrown type is T, a class derived from T, or convertible via a pointer/standard conversion. Catching by value slices derived objects. |
catch (...) |
Matches any exception. It must be the last handler. |
Usage notes
- Handlers are tried in order, so put derived classes before their bases. A handler for a base class placed first hides later handlers; compilers warn.
- Catch by
const&. Catching by value copies and slices; catching a pointer to a thrown object invites ownership bugs. - Throw
std::runtime_error,std::logic_error,std::out_of_rangeand friends, or derive your own class fromstd::exceptionso generic handlers work. Throwing built-in types likeintworks but gives callers nothing to inspect. - Destructors are implicitly
noexcept. An exception that escapes a destructor ends the program. Constructors may throw: the members built so far are destroyed. - Do not rely on
catch (...)alone to diagnose; usestd::current_exception()to store an exception andstd::rethrow_exceptionto rethrow it elsewhere (for example from a thread). - A function-try-block on a constructor catches exceptions from the member initializers, but it cannot swallow them: the exception is rethrown automatically at the end of the handler.
- Throwing and catching costs microseconds, so it is slow for hot paths; the no-throw path is almost free on mainstream platforms.
throwinside anoexceptfunction leads tostd::terminate.- For recoverable, expected failures consider
std::optional,std::expected(C++23) or error codes.