Python异常处理

异常处理

介绍

语法错误

语法错误又称解析错误,语法错误是在语法层面出现的代码错误,python解释器可以在代码运行之前对语法错误进行检查,并输出出现语法错误的具体位置和错误原因。

异常

  • 概念
    即使在语法上是正确的,但在尝试执行时,它仍可能会引发错误。在执行时检测到的错误被称为异常,异常不一定会导致严重后果,我们可以利用异常处理来解决异常。

  • 基本语法

1
2
3
4
5
6
try:
pass
except Exception as e:
pass
finally:
pass
  • 示例
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    # 定义除法的函数
    def divide(x, y):
    try:
    result = x / y
    except ZeroDivisionError: # 如果发生ZeroDivisionError异常,则进行下面的处理
    print("division by zero!")
    else: # 如果程序正常执行,则输出除法结果
    print("result is", result)
    finally: # 定义清理操作,程序不论是否触发异常,一定会执行finally,通常进行清理操作
    print("executing finally clause")

    divide(1,0) # 函数调用

    # 输出
    division by zero!
    executing finally clause

异常种类

常见异常

异常名称 描述
BaseException 所有异常的基类
SystemExit 解释器请求退出
KeyboardInterrupt 用户中断执行(通常是输入^C)
Exception 常规错误的基类
StopIteration 迭代器没有更多的值
GeneratorExit 生成器(generator)发生异常来通知退出
StandardError 所有的内建标准异常的基类
ArithmeticError 所有数值计算错误的基类
FloatingPointError 浮点计算错误
OverflowError 数值运算超出最大限制
ZeroDivisionError 除(或取模)零(所有数据类型)
AssertionError 断言语句失败
AttributeError 对象没有这个属性
EOFError 没有内建输入,到达EOF标记
EnvironmentError 操作系统错误的基类
IOError 输入/输出操作失败
OSError 操作系统错误
WindowsError 系统调用失败
ImportError 导入模块/对象失败
LookupError 无效数据查询的基类
IndexError 序列中没有此索引(index)
KeyError 映射中没有这个键
MemoryError 内存溢出错误(对于Python解释器不是致命的)
NameError 未声明/初始化对象(没有属性)
UnboundLocalError 访问未初始化的本地变量
ReferenceError 弱引用(Weakreference)试图访问已经垃圾回收了的对象
RuntimeError 一般的运行时错误
NotImplementedError 尚未实现的方法
SyntaxError Python 语法错误
IndentationError 缩进错误
TabError Tab和空格混用
SystemError 一般的解释器系统错误
TypeError 对类型无效的操作
ValueError 传入无效的参数
UnicodeErrorUnicode 相关的错误
UnicodeDecodeErrorUnicode 解码时的错误
UnicodeEncodeErrorUnicode 编码时错误
UnicodeTranslateErrorUnicode 转换时错误
Warning 警告的基类
DeprecationWarning 关于被弃用的特征的警告
FutureWarning 关于构造将来语义会有改变的警告
OverflowWarning 旧的关于自动提升为长整型(long)的警告
PendingDeprecationWarning 关于特性将会被废弃的警告
RuntimeWarning 可疑的运行时行为(runtimebehavior)的警告
SyntaxWarning 可疑的语法的警告
UserWarning 用户代码生成的警告

异常层次结构

由下图的异常层级结构我们可以看出,BaseException 所有内置异常的基类。但是官方不建议在实际使用时直接继承使用,而是建议继承Exception类,几乎所有的异常都是Exception的子类。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
BaseException
+-- SystemExit
+-- KeyboardInterrupt
+-- GeneratorExit
+-- Exception
+-- StopIteration
+-- StopAsyncIteration
+-- ArithmeticError
| +-- FloatingPointError
| +-- OverflowError
| +-- ZeroDivisionError
+-- AssertionError
+-- AttributeError
+-- BufferError
+-- EOFError
+-- ImportError
| +-- ModuleNotFoundError
+-- LookupError
| +-- IndexError
| +-- KeyError
+-- MemoryError
+-- NameError
| +-- UnboundLocalError
+-- OSError
| +-- BlockingIOError
| +-- ChildProcessError
| +-- ConnectionError
| | +-- BrokenPipeError
| | +-- ConnectionAbortedError
| | +-- ConnectionRefusedError
| | +-- ConnectionResetError
| +-- FileExistsError
| +-- FileNotFoundError
| +-- InterruptedError
| +-- IsADirectoryError
| +-- NotADirectoryError
| +-- PermissionError
| +-- ProcessLookupError
| +-- TimeoutError
+-- ReferenceError
+-- RuntimeError
| +-- NotImplementedError
| +-- RecursionError
+-- SyntaxError
| +-- IndentationError
| +-- TabError
+-- SystemError
+-- TypeError
+-- ValueError
| +-- UnicodeError
| +-- UnicodeDecodeError
| +-- UnicodeEncodeError
| +-- UnicodeTranslateError
+-- Warning
+-- DeprecationWarning
+-- PendingDeprecationWarning
+-- RuntimeWarning
+-- SyntaxWarning
+-- UserWarning
+-- FutureWarning
+-- ImportWarning
+-- UnicodeWarning
+-- BytesWarning
+-- ResourceWarning

抛出异常

概念

raise 语句允许程序员强制发生指定的异常。

示例

1
2
3
4
5
try:
raise NameError('HiThere') # 主动抛出异常
except NameError:
print('An exception flew by!')
raise

自定义异常

概念

程序可以通过创建新的异常类来命名它们自己的异常。异常通常应该直接或间接地从 Exception 类派生。自定义异常类通常要保持功能简单,通常只提供许多属性,这些属性允许处理程序为异常提取有关错误的信息。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Error(Exception):  # 继承Exception
"""Base class for exceptions in this module."""
pass


class TransitionError(Error): # 继承Exception的子类Error
"""Raised when an operation attempts a state transition that's not
allowed.

Attributes:
previous -- state at beginning of transition
next -- attempted new state
message -- explanation of why the specific transition is not allowed
"""

def __init__(self, previous, next, message):
self.previous = previous
self.next = next
self.message = message

断言

概念

assert 语句是在程序中插入调试性断言的简便方式

断言的两种形式

  • 简单形式:assert expression
1
2
3
# 简单形式断言,等价于
if __debug__:
if not expression: raise AssertionError
  • 扩展形式:assert expression1, expression2
1
2
3
#扩展形式断言,等价于
if __debug__:
if not expression1: raise AssertionError(expression2)

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def foo():
try:
assert 1 == 1 # 断言1 == 1
print('【NO·1】断言成功时执行')
assert 2 == 1
print('【NO·2】断言失败时执行') # 断言此处会失败,进而抛出AssertionError异常
except Exception: # 捕获AssertionError异常
print("Catch AssertionError ")

foo()

# 输出

【NO·1】断言成功时执行
Catch AssertionError