Customizing termination behavior for uncaught exception In C++ Last Updated : 15 Apr, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report Whenever an exception arises in C++, it is handled as per the behavior defined using the try-catch block. However, there is often the case when an exception is thrown but isn't caught because the exception handling subsystem fails to find a matching catch block for that particular exception. In that case, the following set of actions takes place: The exception handling subsystem calls the function: unexpected(). This function, provided by the default C++ library, defines the behavior when an uncaught exception arises. By default, unexpected calls terminate(). The terminate function defines the actions that are to be performed during process termination. This, by default, calls abort(). The process is aborted. The terminate() and unexpected() simply call other functions to actually handle an error. As explained above, terminate calls abort(), and unexpected() calls terminate(). Thus, both functions halt the program execution when an exception handling error occurs. However, you can change the way termination occurs. To change the terminate handler, the function used is set_terminate(terminate_handler newhandler), which is defined in the header <exception>. The following program demonstrates how to set a custom termination handler: CPP // CPP program to set a new termination handler // for uncaught exceptions. #include <exception> #include <iostream> using namespace std; // definition of custom termination function void myhandler() { cout << "Inside new terminate handler\n"; abort(); } int main() { // set new terminate handler set_terminate(myhandler); try { cout << "Inside try block\n"; throw 100; } catch (char a) // won't catch an int exception { cout << "Inside catch block\n"; } return 0; } Output: Inside try block Inside new terminate handler Runtime Error: Abort signal from abort(3) (SIGABRT) It is to be duly noted that the only thing that your custom terminate handler must do is stop the program execution. It must not return to the program or resume it in any way. Comment More infoAdvertise with us Next Article Customizing termination behavior for uncaught exception In C++ P ParthDutt Follow Improve Article Tags : C++ cpp-exception Practice Tags : CPP Similar Reads User-defined Custom Exception with class in C++ We can use Exception handling with class too. Even we can throw an exception of user defined class types. For throwing an exception of say demo class type within try block we may write throw demo(); Example 1: Program to implement exception handling with single class CPP14 #include <iostream> 3 min read exception::bad_exception in C++ with Examples Standard C++ contains several built-in exception classes, exception::bad_exception is one of them. This is an exception thrown by unexpected handler. Below is the syntax for the same: Header File: include<exception> Syntax: class bad_exception; Return: The exception::bad_exception returns a nu 2 min read Comparison of Exception Handling in C++ and Java Both languages use to try, catch and throw keywords for exception handling, and their meaning is also the same in both languages. Following are the differences between Java and C++ exception handling: Java C++ Only throwable objects can be thrown as exceptions.All types can be thrown as exceptions.W 4 min read Exception Handling and Object Destruction in C++ An exception is termed as an unwanted error that arises during the runtime of the program. The practice of separating the anomaly-causing program/code from the rest of the program/code is known as Exception Handling. An object is termed as an instance of the class which has the same name as that of 4 min read Rethrowing an Exception in C++ Exception handling plays a role in developing robust software in C++. It offers a way to handle errors and unexpected situations. One interesting aspect of exception handling in C++ is the ability to rethrow an exception allowing it to pass up the call stack. Rethrowing an ExceptionRethrowing an exc 5 min read Like