Exception Handling in C++ is a process to handle runtime errors. We perform exception handling so the normal flow of the application can be maintained even after runtime errors.
In C++, exception is an event or object which is thrown at runtime. All exceptions are derived from std::exception class. It is a runtime error which can be handled. If we don't handle the exception, it prints exception message and terminates the program.
It maintains the normal flow of the application. In such case, rest of the code is executed even after exception.
In C++ standard exceptions are defined in <exception> class that we can use inside our programs. The arrangement of parent-child class hierarchy is shown below:
All the exception classes in C++ are derived from std::exception class. Let's see the list of C++ common exception classes.
||It is an exception and parent class of all standard C++ exceptions.|
||It is an exception that can be detected by reading a code.|
||It is an exception that cannot be detected by reading a code.|
||It is used to handle the unexpected exceptions in a c++ program.|
||This exception is generally be thrown by dynamic_cast.|
||This exception is generally be thrown by typeid.|
||This exception is generally be thrown by new.|
In C++, we use 3 keywords to perform exception handling:
Moreover, we can create user-defined exception.