Does an excluded exception in a thread exclude only the thread or the whole process?

When an exception is thrown inside the thread without catching it elsewhere, will it delete the entire application / interpreter / process? Or will he just kill the stream?

+8
python multithreading exception
source share
2 answers

Try:

import threading import time class ThreadWorker(threading.Thread): def run(self): print "Statement from a thread!" raise Dead class Main: def __init__(self): print "initializing the thread" t = ThreadWorker() t.start() time.sleep(2) print "Did it work?" class Dead(Exception): pass Main() 

The above code gives the following results:

 > initializing the thread > Statement from a thread! > Exception in thread > Thread-1: Traceback (most recent call last): File > "C:\Python27\lib\threading.py", line 551, in __bootstrap_inner > self.run() File ".\pythreading.py", line 8, in run > raise Dead Dead > ----- here the interpreter sleeps for 2 seconds ----- > Did it work? 

So, the answer to your question is that the raised exception causes only the thread in which it is located, and not the entire program.

+11
source share

From the threading document:

Once the thread activity is started, the thread is considered 'alive.' It ceases to be alive when its run () method completes - either in normal mode or by throwing an unhandled exception. The Is_alive () method checks if the thread is alive.

And:

join (timeout = no)

Wait for the thread to complete. This blocks the calling thread until the thread whose join () method completes, either typically through an unhandled exception, or until the wait time.

In other words, an uncaught exception is a way to terminate a thread and will be detected in the parent join call for the specified thread.

+5
source share

All Articles