We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
程序未免会发生异常,于是想出了一个招!叫捕获异常
Python的异常很多,比如我想运算1/'qqq',那么程序会抛出TypeError,说明1和'qqq'不是一个类型(Type),无法运算!
1/'qqq'
TypeError
于是我们需要一个try语句来尝试1/'qqq',这样程序是允许的,然后跟上[except],其实类似[else]的含义,就是如果try成功执行,就不走[except]了
try
except
else
格式:
try: 尝试语句 except 异常类型: 异常了怎么办? 可以多重except,就是侦测多个异常
实例:
n = 100 try: print(100 / 'QQQ') except TypeError as te: print("TypeError!") print(te) except ValueError as ve: print("ValueError!") print(ve)
其中ve和te你不用管,这是输出异常信息
最后输出:
TypeError! unsupported operand type(s) for /: 'int' and 'str'
The text was updated successfully, but these errors were encountered:
挺不错的吧
Sorry, something went wrong.
No branches or pull requests
程序未免会发生异常,于是想出了一个招!叫捕获异常
Python的异常很多,比如我想运算
1/'qqq'
,那么程序会抛出TypeError
,说明1和'qqq'不是一个类型(Type),无法运算!于是我们需要一个
try
语句来尝试1/'qqq'
,这样程序是允许的,然后跟上[except
],其实类似[else
]的含义,就是如果try
成功执行,就不走[except
]了格式:
实例:
其中ve和te你不用管,这是输出异常信息
最后输出:
The text was updated successfully, but these errors were encountered: