【发布时间】:2017-07-31 19:58:32
【问题描述】:
我有以下文件:
from fabric.api import env, execute, run
env.hosts = ['1.2.3.4']
def taskA():
run('ls')
def main():
try:
execute(taskA)
except:
print "Exception Caught"
main()
当我运行此程序时,我能够看到打印的“异常捕获”:
$ python test.py
[1.2.3.4] Executing task 'taskA'
[1.2.3.4] run: ls
Fatal error: Timed out trying to connect to 1.2.3.4 (tried 1 time)
Underlying exception:
timed out
Aborting.
Exception Caught
但是,当我把它切换到这个时:
def main():
try:
execute(taskA)
except Exception, e:
print "Exception Caught", e
main()
我没有看到异常被捕获:
[1.2.3.4] run: ls
Fatal error: Timed out trying to connect to 1.2.3.4 (tried 1 time)
Underlying exception:
timed out
Aborting.
我能在上面而不是下面的代码中捕捉到错误是否有原因?
【问题讨论】:
-
我没有看到回溯,所以异常被捕获。你确定你打印正确吗?另外,请使用
except Exception as e:。 -
@MartijnPieters:没有回溯可能意味着它是 SystemExit。
-
@user2357112 好点!