Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If an exception occurs in a thread, the best way is to re-raise it in the caller thread during <code>join</code>. You can get information about the exception currently being handled using the <code>sys.exc_info()</code> function. This information can simply be stored as a property of the thread object until <code>join</code> is called, at which point it can be re-raised.</p> <p>Note that a <code>Queue.Queue</code> (as suggested in other answers) is not necessary in this simple case where the thread <em>throws at most 1 exception</em> and <em>completes right after throwing an exception</em>. We avoid race conditions by simply waiting for the thread to complete.</p> <p>For example, extend <code>ExcThread</code> (below), overriding <code>excRun</code> (instead of <code>run</code>).</p> <p><strong>Python 2.x:</strong></p> <pre><code>import threading class ExcThread(threading.Thread): def excRun(self): pass def run(self): self.exc = None try: # Possibly throws an exception self.excRun() except: import sys self.exc = sys.exc_info() # Save details of the exception thrown but don't rethrow, # just complete the function def join(self): threading.Thread.join(self) if self.exc: msg = "Thread '%s' threw an exception: %s" % (self.getName(), self.exc[1]) new_exc = Exception(msg) raise new_exc.__class__, new_exc, self.exc[2] </code></pre> <p><strong>Python 3.x:</strong></p> <p>The 3 argument form for <code>raise</code> is gone in Python 3, so change the last line to:</p> <pre><code>raise new_exc.with_traceback(self.exc[2]) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload