Python 含有异常处理机制
来帮助用户处理可能发生的错误异常。
1. 异常概念
异常是指Python程序运行过程中遇到的错误, 每个异常都是某个异常类的实例
。下面实例中未对异常做任何处理,导致程序终止并且抛出错误信息。其中TypeError
是异常类中的一种。
def test(a=1, b='2'):
return a + b
test()
#result
Traceback (most recent call last):
File "test.py", line 4, in <module>
test()
File "test.py", line 2, in test
return a + b
TypeError: unsupported operand type(s) for +: 'int' and 'str'
2. 抛出异常
Python中抛出异常必须用到raise
语句, 后面接上异常类
(会自动创建实例)或者异常类的实例
。
raise Exception
#result
Traceback (most recent call last):
File "test.py", line 1, in <module>
raise Exception
Exception
#############################
raise Exception()
#result
Traceback (most recent call last):
File "test.py", line 1, in <module>
raise Exception
Exception
#############################
raise Exception('Meet Error.')
#result
Traceback (most recent call last):
File "test.py", line 1, in <module>
raise Exception('Meet Error.')
Exception: Meet Error.
抛出异常的进阶:
def test(a=1, b='2'):
return a + b
try:
test()
except Exception as e:
raise
#raise RuntimeError('Raise Error.')
#raise RuntimeError('Raise Error.') from e
#raise RuntimeError('Raise Error.') from None
#result 1
Traceback (most recent call last):
File "test.py", line 5, in <module>
test()
File "test.py", line 2, in test
return a + b
TypeError: unsupported operand type(s) for +: 'int' and 'str'
#result 2
Traceback (most recent call last):
File "test.py", line 5, in <module>
test()
File "test.py", line 2, in test
return a + b
TypeError: unsupported operand type(s) for +: 'int' and 'str'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 8, in <module>
raise RuntimeError('Raise Error.')
RuntimeError: Raise Error.
#result 3
Traceback (most recent call last):
File "test.py", line 5, in <module>
test()
File "test.py", line 2, in test
return a + b
TypeError: unsupported operand type(s) for +: 'int' and 'str'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "test.py", line 9, in <module>
raise RuntimeError('Raise Error.') from e
RuntimeError: Raise Error.
#result 4
Traceback (most recent call last):
File "test.py", line 10, in <module>
raise RuntimeError('Raise Error.') from None
RuntimeError: Raise Error.
- 第一种: 直接raise, 捕获异常后,
重新引发
该异常 - 第二种: 抛出RuntimeError,
处理异常时发生了新的异常
,更倾向于新异常与正在处理的异常没有关系 - 第三种: from e, 指出
新异常是因旧异常直接引起
的, 有助于后续对异常的分析和排查 - 第四种: from None, 明确
禁止异常关联
3. 异常类
异常类分两种, 一是内置异常类
,如Exception
, OSError
,SyntaxError
等, 直接拿来就可以用。
BaseException
是所有异常类的基类。详见内置异常。
二是为满足个人需求的自定义异常类
, 这种类必须要直接或间接地继承 Exception
, 然后根据需求是否添加方法。
class CaseError(Exception):
pass
raise CaseError('Case is error.')
#result
Traceback (most recent call last):
File "test.py", line 4, in <module>
raise CaseError('Case is error.')
__main__.CaseError: Case is error.
4. 捕获异常
可以使用try/except
来处理可能发生异常的语句。
4.1 捕获一种
异常
使用try/except
+ 异常类名
来捕获该异常
def test(a=1, b='2'):
return a + b
try:
test()
except TypeError:
print ('Meet type error.')
#result
Meet type error.
4.2 捕获多种
异常
有时候程序往往出现不止一种异常, 那如何捕获多个异常呢,有下面的两种方法。
- 多层次捕获: 层次分明, 针对不同的异常做不同的处理
注意
:后面第二个至第n个类中不能是第一个的子类, 否则该异常永远都无法被捕获
def test(a=1, b='2'):
return a + b
try:
test()
except TypeError:
print ('It is type error.')
except NameError:
print ('It is name error.')
#result
It is type error.
- 一起捕获: 把所有的异常类放在一个元祖里面
def test(a=1, b='2'):
return a + b
try:
test()
except (TypeError, NameError):
print ('Meet error.')
#result
Meet error.
4.3 捕获所有
异常
捕获所有异常有两种常见的格式, 分别是try/except
和 try/except Exception
。
-
try/except
会捕获所有异常,包含键盘中断(KeyboardInterrupt)
和程序退出请求(SystemExit)
,慎用之。可能会使sys.exit() 语句被捕获,导致无法退出脚本。
import sys
try:
sys.exit(0)
print ('Exit the function.')
except:
print ('Meet error.')
#result
Meet error.
# sleep 的时候,按Ctrl+c
import time
try:
time.sleep(20)
print ('Sleep 20s.')
except:
print ('Meet error.')
#result,^C表示Ctrl+c
^CMeet Error.
-
try/except Exception
会捕获大部分异常,除了键盘中断(KeyboardInterrupt)
和程序退出请求(SystemExit)
,因为它们是从 BaseException ( Exception 的超类)派生而来的。
import sys
try:
sys.exit(0)
print ('Exit the function.')
except Exception:
print ('Meet error.')
#result
退出脚本
###############
import time
try:
time.sleep(20)
print ('Sleep 20s.')
except Exception:
print ('Meet error.')
#result
^CTraceback (most recent call last):
File "test.py", line 4, in <module>
time.sleep(20)
KeyboardInterrupt
5. 获取更多的异常信息
当捕获异常的时候, 我们需要获取更多的信息去定位和解决
问题。
这里有三种方式。一是try/except XXX as e
, 另外是traceback
内置模块,第三是logging
内置模块。
1. e表示异常对象
2. print_exc():对异常的输出
3. format_exc():把异常以字符串的形式返回,print(traceback.format_exc())等同于于traceback.print_exc()
4. print_exception():traceback.print_exc()实现方式就是traceback.print_exception(sys.exc_info())
import sys
import logging
import traceback
def test(a=1, b='2'):
return a + b
try:
test()
except Exception as e:
print ('****** print object e ******')
print ('Exception type is %s.' % type(e))
print ('e is %s.' % e)
print ('****** traceback.print_exc() ******')
traceback.print_exc()
print ('****** traceback.format_exc() ******')
print(traceback.format_exc())
print ('****** traceback.print_exception() ******')
traceback.print_exception(*sys.exc_info())
print ('****** logging_exception() ******')
traceback.print_exception(*sys.exc_info())
###########result###########
****** print object e ******
Exception type is <class 'TypeError'>.
e is unsupported operand type(s) for +: 'int' and 'str'.
****** traceback.print_exc() ******
Traceback (most recent call last):
File "test.py", line 8, in <module>
test()
File "test.py", line 5, in test
return a + b
TypeError: unsupported operand type(s) for +: 'int' and 'str'
****** traceback.format_exc() ******
Traceback (most recent call last):
File "test.py", line 8, in <module>
test()
File "test.py", line 5, in test
return a + b
TypeError: unsupported operand type(s) for +: 'int' and 'str'
****** traceback.print_exception() ******
Traceback (most recent call last):
File "test.py", line 8, in <module>
test()
File "test.py", line 5, in test
return a + b
TypeError: unsupported operand type(s) for +: 'int' and 'str'
****** logging_exception() ******
Traceback (most recent call last):
File "test.py", line 9, in <module>
test()
File "test.py", line 6, in test
return a + b
TypeError: unsupported operand type(s) for +: 'int' and 'str'
6. else 和 finally
try/except ... else
举例: else 在主 try 块没有引发异常时执行
def except_test():
try:
print ('Running')
raise Exception('exception raised')
return 0
except Exception:
print('Here have exception')
return 1
else:
print('No exception')
return 2
print (except_test())
#如果没有发生异常, 则返回
Running
0
#如果没有发生异常,且注释掉return 0, 则返回
Running
No exception
2
#如果发生异常,则返回
Running
Here have exception
1
#如果发生异常,且注释掉return 1, 则返回
Running
Here have exception
None
由此可见:
- try 中有异常,会直接跳到except语句
- try 中没有异常, 且没有return,才会跳到else语句
try/except ... else ... finally
举例:无论是否引发异常都将执行
def except_test():
try:
print ('Running')
raise Exception('exception raised')
return 0
except Exception:
print('Here have exception')
return 1
else:
print('No exception')
return 2
finally:
print ('Do clean work')
return 3
print (except_test())
#如果没有发生异常, 则返回
Running
Do clean work
3
#如果没有发生异常,且注释掉return 0, 则返回
Running
No exception
Do clean work
3
#如果没有发生异常,且注释掉return 3, 则返回
Running
Do clean work
0
#如果没有发生异常,且注释掉return 0 和 return 3, 则返回
Running
No exception
Do clean work
2
#如果发生异常, 则返回
Running
Here have exception
Do clean work
3
#如果发生异常, 且注释掉return 3,则返回
Running
Here have exception
Do clean work
1
由此可知:
- try 中不管有没有异常, 有没有return, 最终都会运行finally, 且如果finally 中有return,则函数返回该return
- finally return 优先级》try/except return 优先级》else return
7. 异常是向上传播的
在函数中引发异常时,异常将传播到调用函数的地方。
def err():
raise Exception('Error.')
def test_err():
err()
def test_err_too():
test_err()
test_err_too()
#result
Traceback (most recent call last):
File "test.py", line 10, in <module>
test_err_too()
File "test.py", line 8, in test_err_too
test_err()
File "test.py", line 5, in test_err
err()
File "test.py", line 2, in err
raise Exception('Error.')
Exception: Error.