Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when an exception goes unhandled in a multithreaded C++11 program?

If I have a C++11 program running two threads, and one of them throws an unhandled exception, what happens? Will the entire program die a fiery death? Will the thread where the exception is thrown die alone (and if so, can I obtain the exception in this case)? Something else entirely?

like image 265
R. Martinho Fernandes Avatar asked Sep 01 '11 14:09

R. Martinho Fernandes


People also ask

What happens if there is exception in thread?

An uncaught exception will cause the thread to exit. When it bubbles to the top of Thread. run() it will be handled by the Thread's UncaughtExceptionHandler. By default, this will merely print the stack trace to the console.

How do you handle an unhandled exception in the thread?

Uncaught exception handler will be used to demonstrate the use of exception with thread. It is a specific interface provided by Java to handle exception in the thread run method. There are two methods to create a thread: Extend the thread Class (java.

What happens if an exception goes uncaught?

If an exception is not caught, it is intercepted by a function called the uncaught exception handler. The uncaught exception handler always causes the program to exit but may perform some task before this happens. The default uncaught exception handler logs a message to the console before it exits the program.

What happens if a thread throws an exception C++?

When a thread throws an exception, it is not caught by main.


1 Answers

Nothing has really changed. The wording in n3290 is:

If no matching handler is found, the function std::terminate() is called

The behavior of terminate can be customized with set_terminate, but:

Required behavior: A terminate_handler shall terminate execution of the program without returning to the caller.

So the program exits in such a case, other threads cannot continue running.

like image 107
Ben Voigt Avatar answered Oct 14 '22 09:10

Ben Voigt