非常简单的python 3尝试除了ValueError而不是跳闸

Aar*_*ron 0 python try-catch python-3.x

试图让我的尝试除了阻止工作.

import sys

def main():
    try:
        test = int("hello")
    except ValueError:
        print("test")
        raise

main()
Run Code Online (Sandbox Code Playgroud)

输出是

C:\Python33>python.exe test.py
test
Traceback (most recent call last):
  File "test.py", line 10, in <module>
    main()
  File "test.py", line 5, in main
    test = int("hello")
ValueError: invalid literal for int() with base 10: 'hello'

C:\Python33>
Run Code Online (Sandbox Code Playgroud)

我希望除了旅行

Mar*_*ers 6

你正在重新启动例外.它按设计工作.

test是正确的回溯之前的顶部有印刷,但你用raise这样的例外仍然是造成回溯:

>>> def main():
...     try:
...         test = int("hello")
...     except ValueError:
...         print("test")
...         raise
... 
>>> main()
test
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in main
ValueError: invalid literal for int() with base 10: 'hello'
Run Code Online (Sandbox Code Playgroud)

删除raise和只是test打印遗骸:

>>> def main():
...     try:
...         test = int("hello")
...     except ValueError:
...         print("test")
... 
>>> main()
test
Run Code Online (Sandbox Code Playgroud)