Scope of try...catch constructions (over threads)
-
Hello all, I was wondering about the scope of try...catch constructions over threads. An example (very simplified example of existing code, where the Main_thread acts like a scheduler and the Worker_Threads are the 'processes'):
Main_thread
{
try {
// Launch some Worker_threads
}
catch( ... ) {
// Do some actions
}
}Worker_thread
{
try {
// The executing code, which might also execute
// the throw-function.
}
catch( condition ) {
// Do some actions, but continue operation
}
catch( ... ) {
// Do some actions
throw( something ); // should be catched in Main_Thread
}
}Is it possible to (or 'how to') catch the last throw from the Worker_thread into the Main_thread? What is the nice way to do this? Thanks in advance, EiSl
-
Hello all, I was wondering about the scope of try...catch constructions over threads. An example (very simplified example of existing code, where the Main_thread acts like a scheduler and the Worker_Threads are the 'processes'):
Main_thread
{
try {
// Launch some Worker_threads
}
catch( ... ) {
// Do some actions
}
}Worker_thread
{
try {
// The executing code, which might also execute
// the throw-function.
}
catch( condition ) {
// Do some actions, but continue operation
}
catch( ... ) {
// Do some actions
throw( something ); // should be catched in Main_Thread
}
}Is it possible to (or 'how to') catch the last throw from the Worker_thread into the Main_thread? What is the nice way to do this? Thanks in advance, EiSl
Is it possible to (or 'how to') catch the last throw from the Worker_thread into the Main_thread? The short answer is no you can't. Exceptions are intrinsically attached to the thread where they're produced, as are the mechanisms of stack unwinding involved. An uncaught exception is simply swallowed by the system (though it is no good practice to let this happen). An approach to having uncaught exceptions somewhat notified to the main thread is having a grand wrap-all
try-catch
in the worker thread that intercepts otherwise uncaught exceptions, translates this to aDWORD
and return that result code, which the main thread can access withGetExitCodeThread()
. How do you translate exceptions toDWORD
s? Depends on what kind of exceptions you're dealing with:- If you don't have any a priori idea about the exceptions the worker thread can throw, then there's little you can do except perhaps returning 0 as "everything OK" and some other code for "some exception thrown".
- If the exceptions are derived from C++
std::exception
, which provide a description with methodwhat()
, you can store this message in some global table and return a pointer to it. - If the exceptions are copyable, you can store the exception objet itself in a table an return a pointer to it. Please note that this requires that the exceptions be of an exact known type, as derived types objects are not handled correctly (they're sliced).
- Some other ad hoc mechanism. You name it.
Hope this helped. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
-
Hello all, I was wondering about the scope of try...catch constructions over threads. An example (very simplified example of existing code, where the Main_thread acts like a scheduler and the Worker_Threads are the 'processes'):
Main_thread
{
try {
// Launch some Worker_threads
}
catch( ... ) {
// Do some actions
}
}Worker_thread
{
try {
// The executing code, which might also execute
// the throw-function.
}
catch( condition ) {
// Do some actions, but continue operation
}
catch( ... ) {
// Do some actions
throw( something ); // should be catched in Main_Thread
}
}Is it possible to (or 'how to') catch the last throw from the Worker_thread into the Main_thread? What is the nice way to do this? Thanks in advance, EiSl