简体   繁体   English

Python:为什么退出后仍会调用except

[英]Python: why except is called even after exit

Please check the below code, 请检查以下代码,

import sys
try:
    seq=eval(raw_input("Enter seq number: "))
    if seq <= 0 or seq >= 9999:
       print "Sequence number not in range [0001-9999]"
       sys.exit(1)
except:
      print "!!! Sequence number not in range [0001-9999]"
      sys.exit(1)

I gave a string to eval raw_input function. 我给eval raw_input函数提供了一个字符串。

$> python test.py
Enter seq number: "12"


Sequence number not in range [0001-9999]
!!! Sequence number not in range [0001-9999]

Why is it not exiting even after receiving exit call? 为什么即使接到exit呼叫后仍不退出?

sys.exit just raises an exception ( SystemExit ), which is then caught. sys.exit只会引发一个异常( SystemExit ),然后将其捕获。 As a demonstration: 作为演示:

import sys
import traceback

try:
    sys.exit(1)
except:
    print "This threw the following exception:"
    traceback.print_exc()
# This threw the following exception:
# Traceback (most recent call last):
#   File "test.py", line 5, in <module>
#     sys.exit(1)
# SystemExit: 1

sys.exit raises the SystemExit exception which is caught by your unnamed exception handler sys.exit引发SystemExit异常,该异常由未命名的异常处理程序捕获

Note, it is usually not a good idea to have a generic exception handler as its evident here. 注意,在此处具有通用异常处理程序通常不是一个好主意。

So as to avoid catching the SystemExit with your generic exception handler, add another exception handler to handler your SystemExit 为了避免使用您的通用异常处理程序捕获SystemExit ,请添加另一个异常处理程序以处理您的SystemExit

>>> try:
    seq=eval(raw_input("Enter seq number: "))
    if seq <= 0 or seq >= 9999:
       print "Sequence number not in range [0001-9999]"
       sys.exit(1)
except SystemExit:
    pass
except Exception:
      print "!!! Sequence number not in range [0001-9999]"
      sys.exit(1)

This is an excellent case why you should never use a bare except. 这是一个绝妙的案例,为什么您不应该使用裸机。 Invalid numbers are ValueErrors, so: 无效的数字是ValueErrors,因此:

import sys
try:
    seq = int(raw_input("Enter seq number: "))
    if seq <= 0 or seq >= 9999:
       raise ValueError('sequence number not in range [0001-9999]')
except ValueError as e:
    print e
    sys.exit(1)

Output: 输出:

C:\>test
Enter seq number: 10000
sequence number not in range [0001-9999]

C:\>test
Enter seq number: abc
invalid literal for int() with base 10: 'abc'

C:\>test
Enter seq number: 5

Note eval is also frowned upon, because it will execute whatever the user types, such as import shutil; shutil.rmtree('/') 注意, eval也不适用,因为它将执行任何用户类型的操作,例如import shutil; shutil.rmtree('/') import shutil; shutil.rmtree('/') . import shutil; shutil.rmtree('/')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么这除了被称为? -Python - Why is this except being called? - Python Python 为什么这个 for 循环退出时出现异常? - Python Why does this for loop exit with with the except? 为什么 sys.exit() 在 Python 中的线程内部调用时不退出? - Why does sys.exit() not exit when called inside a thread in Python? 为什么finally块在调用除块之外的sys.exit(0)后执行? - Why finally block is executing after calling sys.exit(0) in except block? Python - 1秒后我无法退出GUI,当调用gtk.main()时它永远不会退出,我该如何退出? - Python - After 1 second i cant exit the GUI, when ever the gtk.main() is called it never exit, how can i exit? 即使在python多线程环境中调用release()之后,.locked()仍返回True - .locked() returns True even after release() called in python multithreading environment 为什么 python 编译器在 exit() 后不忽略语法错误? - why python compiler doesn't ignore syntax errors after exit()? 为什么它的工作方式不同,即使它在 python 尝试中几乎相同的代码除外? - Why does it work differently even though it's almost the same code in python try except? 即使在使用 try 和 except 之后 ZeroDivisionError - ZeroDivisionError even after using try and except 为什么Python调用sys.exit时upstart服务不会停止 - Why upstart service does not stop when sys.exit is called by Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM