throw keyword from inside the try block. Exception handlers are declared with the keyword catch, which must be placed immediately after the try block: |
|
An exception occurred. Exception Nr. 20 |
try block. In this example this code simply throws an exception: |
|
throw expression accepts one parameter (in this case the integer value 20), which is passed as an argument to the exception handler.catch keyword immediately after the closing brace of the try block. The syntax for catch is similar to a regular function with one parameter. The type of this parameter is very important, since the type of the argument passed by the throw expression is checked against it, and only in the case they match, the exception is caught by that handler.catch expressions) can be chained; each one with a different parameter type. Only the handler whose argument type matches the type of the exception specified in the throw statement is executed....) is used as the parameter of catch, that handler will catch any exception no matter what the type of the exception thrown. This can be used as a default handler that catches all exceptions not caught by other handlers: |
|
int nor char.throw statement!.try-catch blocks within more external try blocks. In these cases, we have the possibility that an internal catch block forwards the exception to its external level. This is done with the expression throw; with no arguments. For example: |
|
throw specifier to it. For example: |
|
myfunction, which takes one argument of type char and returns a value of type double. If this function throws an exception of some type other than int, the function calls std::unexpected instead of looking for a handler or calling std::terminate.throw specifier is left empty with no type, this means that std::unexpected is called for any exception. Functions with no throw specifier (regular functions) never call std::unexpected, but follow the normal path of looking for their exception handler. |
|
std::exception and is defined in the <exception> header. This class has a virtual member function called what that returns a null-terminated character sequence (of type char *) and that can be overwritten in derived classes to contain some sort of description of the exception. |
|
My exception happened. |
& after the type), therefore this catches also classes derived from exception, like our myex object of type myexception.exception class. These are:| exception | description |
|---|---|
bad_alloc | thrown by new on allocation failure |
bad_cast | thrown by dynamic_cast when it fails in a dynamic cast |
bad_exception | thrown by certain dynamic exception specifiers |
bad_typeid | thrown by typeid |
bad_function_call | thrown by empty function objects |
bad_weak_ptr | thrown by shared_ptr when passed a bad weak_ptr |
exception, header <exception> defines two generic exception types that can be inherited by custom exceptions to report errors:| exception | description |
|---|---|
logic_error | error related to the internal logic of the program |
runtime_error | error detected during runtime |
|
|
bad_alloc. Because bad_alloc is derived from the standard base class exception, it can be caught (capturing by reference, captures all related classes).Type conversions | Index | Preprocessor directives |