我有一个简单的程序Base.py
,它测试import
是否能够在模块不存在时抛出异常。默默杀死线程
# Base.py
import threading, os, time
import_is_working = False
class Launch(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.start()
def run(self):
global import_is_working
try:
print "Yes, it got into the 'try' block"
import NON_EXISTENT_MODULE
assert False
except:
print "Great, your python language is working"
import_is_working = True
Launch()
for i in range(500):
time.sleep(0.01)
if import_is_working:
break
if not import_is_working:
print "Your import is not working."
os._exit(4)
有时候,我喜欢另一个模块Main.py
使用此代码:
max% python Base.py
Yes, it got into the 'try' block
Great, your python language is working
max% python Main.py
Yes, it got into the 'try' block
Your import is not working.
max%
这发生在:
# Main.py
import Base
出人意料的是,它不会,当我运行它的工作方式Ubuntu,还有一个干净的CentOS 7.3安装。
只是建议:无论何时捕捉异常,都要将异常(也可能是堆栈跟踪)添加到报告方案中。例如...例外情况除外:... – redlus