std::thread t {someOtherFunction}; // someOtherFunction may throw!!
If someOtherFunction
can throw, then the exception needs to be handled in that function. If not, the default behaviour is to call std::terminate
if the exception "escapes" the thread function.
The std::thread
object construction and std::thread::detach
itself can also throw, so these exceptions need to be handled by the aFunction()
.
The detach()
method throws if thread object is not joinable. So if you wish to avoid handling this exception, you can add a test to see if the thread is joinable before attempting to detach it.
if (t.joinable())
t.detach;
The fact that you are called detach()
indicates that you do not wish to "join" (synchronise with) the thread at a later stage and it will basically take care of itself.
As with most exception handling questions, what are you going to do with the exception that is thrown? If the exception cannot be handled, then either don't catch it or catch it, log it and rethrow it (or call std::terminate
).