In the dynamic world of C++, exceptions are a powerful tool for handling errors. However, sometimes, we may encounter situations where we inadvertently throw exceptions without properly handling them. In such cases, the C++ runtime system comes to our rescue with its default exception handling mechanism. Let's delve into the concept of 'oops' in C++, understand default exception handling, and explore examples to illustrate these concepts.

C++ provides three keywords - 'try', 'catch', and 'throw' - that are essential in exception handling. The 'try' block identifies the section of code where an exception might occur, 'throw' is used to throw an exception, and 'catch' is used to handle the exception. Let's first discuss default exception handling, which comes into play when we use these mechanisms improperly.

Default Exception Handling in C++
When an exception is thrown, and there's no 'catch' block to handle it, or the thrown exception is not caught in any 'catch' block within the function's scope, the C++ runtime system catches the exception and calls the function's 'terminate' handler. This results in the program terminating abnormally.

The 'terminate' function, by default, calls the 'abort' function, which unceremoniously ends the program. While this provides a failsafe mechanism, it's generally not the best way to handle errors in a production environment. Let's explore this with an example:
```cpp
#include In this example, the program will terminate as there's no 'catch' block to handle the thrown integer.

Using 'std::uncaught_exception' and 'std::terminate()'
We can also manually trigger the 'terminate' function using the 'std::terminate()' function if the number of uncaught exceptions exceeds the limit. The 'std::uncaught_exception()' function tells us if there's an uncaught exception. Let's see an example:
```cpp
#include In this case, since we've thrown an exception and there's no 'catch' block in the 'main' function to handle it, the 'uncaught_exception()' function returns 'true', leading to the program's termination.

Customizing 'std::terminate()' Behavior
We can replace the default behavior of 'std::terminate()' with a custom function. This function is set by calling 'std::set_terminate(newHandler)' with a different function as an argument. Let's explore this with an example:
```cpp
#include In this case, instead of abruptly terminating the program, our custom 'my_terminate()' function is called, providing a cleaner way to handle the termination.

Handling 'std::bad_alloc' Exceptions
A 'std::bad_alloc' exception is thrown when a request for memory allocation fails. Let's discuss how to handle this exception and understand the difference between using 'new' and 'new (nothrow)' for memory allocation.




















Using 'new' for Memory Allocation
When using 'new' for memory allocation, if the allocation fails, a 'std::bad_alloc' exception is thrown. We should catch this exception and handle it accordingly. Here's an example:
```cpp
#include In this case, since allocating 100 billion characters is likely to fail, we catch the 'std::bad_alloc' exception and print an error message instead of allowing the program to terminate.
Using 'new (nothrow)' for Memory Allocation
Instead of using 'new', we can use 'new (nothrow)' for memory allocation. If the allocation fails, 'new (nothrow)' returns a null pointer instead of throwing an exception. Let's see an example:
```cpp
#include In this case, if the memory allocation fails, we handle the error by checking if 'p' is null instead of catching an exception.
In conclusion, understanding default exception handling in C++ is crucial for writing robust and reliable software. It helps us to anticipate and manage errors that might occur during the execution of our program. By properly using 'try', 'catch', and 'throw', and knowing when to use 'new' and 'new (nothrow)', we can write code that gracefully handles exceptions and allocates memory efficiently.
Remember, the key to effective exception handling is to identify potential error sources within your code and provide appropriate handling mechanisms. This way, you’ll turn potential disasters into manageable obstacles, making your code more resilient and your development process more agile.