作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
当一个线程内部引发异常而没有在其他任何地方捕获它时,它会杀死整个应用程序/解释器/进程吗?还是只会杀死线程?
最佳答案
让我们试试看:
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()
上面的代码产生以下结果:
> 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?
因此,您的问题的答案是引发的异常只会使它所在的线程崩溃,而不是整个程序。
关于python - 线程内未捕获异常是只杀死线程还是整个进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13513627/
我是一名优秀的程序员,十分优秀!