Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Scope of try...catch constructions (over threads)

Scope of try...catch constructions (over threads)

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    EiSl
    wrote on last edited by
    #1

    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

    J R 2 Replies Last reply
    0
    • E 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

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      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 a DWORD and return that result code, which the main thread can access with GetExitCodeThread(). How do you translate exceptions to DWORDs? 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 method what(), 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

      1 Reply Last reply
      0
      • E 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

        R Offline
        R Offline
        Rui Lopes
        wrote on last edited by
        #3

        IMHO it's not possible. You have to do some thread syncronization between the Main_thread and Worker_thread. ---- Rui Lopes

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups