Python中的异常处理机制与最佳实践
在Python编程中,异常是指程序执行过程中遇到的错误或异常情况。理解异常的基本概念是掌握Python异常处理机制的基础。 异常是指程序在执行过程中遇到的非预期情况,这些情况可能导致程序无法正常继续执行。例如: 在Python中,异常和错误有以下区别: 错误:通常指语法错误或逻辑错误,这些错误会导致程序无法正常运行 异常:程序在运行过程中遇到的非预期情况,这些情况可以被捕获和处理 Python中的异常是通过类层次结构来组织的,所有异常类都继承自 Python提供了许多内置异常,用于表示不同类型的错误情况。以下是一些常见的内置异常: Python的异常处理机制允许我们捕获和处理程序执行过程中发生的异常,从而使程序能够更加健壮和可靠。 Python的异常处理使用 异常处理的执行流程如下: 在Python中,我们可以使用 在 当异常在函数或方法中引发但未被捕获时,异常会向上传播到调用该函数或方法的代码。如果异常一直传播到程序的顶层而未被捕获,程序会终止并显示异常信息。 在Python 3中,我们可以使用 在Python中,我们可以通过继承内置异常类来创建自定义异常。自定义异常可以帮助我们更好地组织和管理代码中的错误情况。 创建自定义异常类的基本步骤: 创建自定义异常时,应遵循以下最佳实践: 自定义异常适用于以下场景: 异常处理是Python编程中的重要部分,正确的异常处理可以使程序更加健壮和可靠。以下是异常处理的最佳实践: EAFP(Easier to Ask for Forgiveness than Permission)是Python中的一种编程模式,它的核心思想是:先尝试执行操作,如果发生异常再处理。这种模式在Python中非常常见,例如: LBYL(Look Before You Leap)是另一种编程模式,它的核心思想是:在执行操作之前先检查条件,如果条件满足再执行操作。例如: 在Python中,EAFP模式通常比LBYL模式更受欢迎,因为它更简洁,并且在并发环境中更安全。 异常处理会对程序的性能产生一定的影响,因此在编写代码时应该考虑以下几点: 异常处理和日志记录是Python编程中的两个重要部分,它们通常一起使用,以确保程序的健壮性和可维护性。 日志记录是指将程序执行过程中的信息记录到文件或其他输出设备中。Python的 在异常处理中,我们通常需要记录异常信息,以便于调试和问题排查。以下是异常处理与日志记录结合的最佳实践: 使用适当的日志级别:根据异常的严重程度,使用适当的日志级别 Python的 异常处理是Python测试中的重要部分,我们需要确保异常处理代码能够正确地捕获和处理异常。 在Python测试中,我们通常使用 测试异常时,应遵循以下最佳实践: 以下是使用 装饰器是Python中的一种高级特性,我们可以使用装饰器来统一处理函数或方法中的异常。 上下文管理器是Python中的一种高级特性,我们可以使用上下文管理器来管理资源和处理异常。 异常处理会对程序的性能产生一定的影响,我们可以通过以下方法来优化异常处理的性能: 文件操作是Python编程中常见的异常处理场景,我们需要处理文件不存在、权限错误等异常。 网络操作是另一个常见的异常处理场景,我们需要处理连接错误、超时错误等异常。 数据库操作是Python编程中常见的异常处理场景,我们需要处理连接错误、查询错误等异常。 API调用是Python编程中常见的异常处理场景,我们需要处理网络错误、API错误等异常。 输入验证是Python编程中常见的异常处理场景,我们需要处理无效输入、类型错误等异常。 本文详细分析了Python中的异常处理机制与最佳实践,包括: Python的异常处理机制是一种强大的错误处理工具,它允许我们捕获和处理程序执行过程中发生的异常,从而使程序更加健壮和可靠。通过本文的学习,我们应该能够: 在实际开发中,我们应该根据具体情况选择合适的异常处理策略,遵循Python的最佳实践,以提高代码的质量和可维护性。同时,我们应该保持学习的态度,关注Python的最新发展,以充分利用Python的强大功能。 Python的异常处理机制是Python语言的重要特性之一,它为我们提供了一种优雅而强大的错误处理方式。通过合理使用异常处理,我们可以编写更加健壮、可靠和可维护的Python代码。 在编写Python代码时,我们应该: 通过遵循这些原则,我们可以充分利用Python的异常处理机制,编写更加健壮、可靠和可维护的Python代码。异常处理不仅是一种错误处理方式,更是一种编程思想,它体现了Python语言的优雅和强大。 希望本文能够帮助读者理解Python的异常处理机制,掌握异常处理的最佳实践,从而在实际开发中编写出更高质量的Python代码。Python中的异常处理机制与最佳实践
1. 异常的基本概念
1.1 异常的定义
1.2 异常与错误的区别
1.3 异常的层次结构
BaseException类。常见的异常类层次结构如下: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
└── BytesWarning1.4 常见的内置异常
# 异常的基本概念示例
# 1. 触发常见异常
print("触发常见异常示例:")
# 除以零 - ZeroDivisionError
try:
result = 10 / 0
except ZeroDivisionError as e:
print(f"ZeroDivisionError: {e}")
# 访问不存在的文件 - FileNotFoundError
try:
with open("non_existent_file.txt", "r") as f:
content = f.read()
except FileNotFoundError as e:
print(f"FileNotFoundError: {e}")
# 类型错误 - TypeError
try:
result = "10" + 5
except TypeError as e:
print(f"TypeError: {e}")
# 值错误 - ValueError
try:
result = int("abc")
except ValueError as e:
print(f"ValueError: {e}")
# 索引越界 - IndexError
try:
lst = [1, 2, 3]
print(lst[5])
except IndexError as e:
print(f"IndexError: {e}")
# 键不存在 - KeyError
try:
dct = {"a": 1, "b": 2}
print(dct["c"])
except KeyError as e:
print(f"KeyError: {e}")
# 名称错误 - NameError
try:
print(undefined_variable)
except NameError as e:
print(f"NameError: {e}")
# 属性错误 - AttributeError
try:
lst = [1, 2, 3]
lst.non_existent_method()
except AttributeError as e:
print(f"AttributeError: {e}")
# 2. 异常层次结构
print("\n异常层次结构示例:")
# 查看异常的基类
print(f"ZeroDivisionError的基类: {ZeroDivisionError.__bases__}")
print(f"ArithmeticError的基类: {ArithmeticError.__bases__}")
print(f"Exception的基类: {Exception.__bases__}")
print(f"BaseException的基类: {BaseException.__bases__}")
# 3. 捕获所有异常
print("\n捕获所有异常示例:")
try:
result = 10 / 0
except Exception as e:
print(f"捕获到异常: {type(e).__name__}: {e}")
# 4. 捕获多个异常
try:
lst = [1, 2, 3]
print(lst[5])
except (IndexError, ZeroDivisionError) as e:
print(f"捕获到异常: {type(e).__name__}: {e}")
# 5. 异常的传递
def function1():
print("function1 开始")
function2()
print("function1 结束")
def function2():
print("function2 开始")
10 / 0
print("function2 结束")
print("\n异常的传递示例:")
try:
function1()
except ZeroDivisionError as e:
print(f"捕获到异常: {e}")2. 异常处理机制
2.1 异常处理的基本语法
try-except语句来实现,基本语法如下:try:
# 可能会引发异常的代码
pass
except ExceptionType1:
# 处理ExceptionType1类型的异常
pass
except ExceptionType2:
# 处理ExceptionType2类型的异常
pass
else:
# 如果没有引发异常,执行这里的代码
pass
finally:
# 无论是否引发异常,都会执行这里的代码
pass2.2 try子句
try子句包含可能会引发异常的代码。当try子句中的代码执行时,如果发生异常,Python会立即停止执行try子句中的剩余代码,并跳转到相应的except子句。2.3 except子句
except子句用于捕获和处理特定类型的异常。一个try语句可以包含多个except子句,每个except子句处理一种或多种类型的异常。2.4 else子句
else子句是可选的,它包含当try子句中没有引发异常时执行的代码。else子句必须位于所有except子句之后。2.5 finally子句
finally子句是可选的,它包含无论是否引发异常都会执行的代码。finally子句通常用于释放资源,例如关闭文件或网络连接。2.6 异常处理的执行流程
try子句中的代码
a. 停止执行try子句中的剩余代码
b. 查找匹配的except子句
c. 如果找到匹配的except子句,执行该子句中的代码
d. 如果没有找到匹配的except子句,异常会向上传递
e. 执行finally子句中的代码
a. 执行else子句中的代码
b. 执行finally子句中的代码# 异常处理机制示例
# 1. 基本的try-except语句
print("基本的try-except语句示例:")
try:
num = int(input("请输入一个整数: "))
result = 10 / num
print(f"结果: {result}")
except ValueError:
print("输入错误,请输入一个有效的整数")
except ZeroDivisionError:
print("错误:不能除以零")
# 2. 带有else子句的try-except语句
print("\n带有else子句的try-except语句示例:")
try:
num = int(input("请输入一个整数: "))
result = 10 / num
except ValueError:
print("输入错误,请输入一个有效的整数")
except ZeroDivisionError:
print("错误:不能除以零")
else:
print(f"计算成功,结果: {result}")
# 3. 带有finally子句的try-except语句
print("\n带有finally子句的try-except语句示例:")
try:
print("尝试打开文件")
f = open("test.txt", "w")
f.write("Hello, World!")
except Exception as e:
print(f"发生异常: {e}")
finally:
print("无论是否发生异常,都会执行finally子句")
if 'f' in locals() and not f.closed:
print("关闭文件")
f.close()
# 4. 捕获所有异常
print("\n捕获所有异常示例:")
try:
num = int(input("请输入一个整数: "))
result = 10 / num
print(f"结果: {result}")
except Exception as e:
print(f"发生异常: {type(e).__name__}: {e}")
# 5. 异常的传递
print("\n异常的传递示例:")
def read_file(filename):
with open(filename, "r") as f:
content = f.read()
return content
def process_data(data):
lines = data.split('\n')
return len(lines)
def main():
try:
data = read_file("non_existent_file.txt")
count = process_data(data)
print(f"文件行数: {count}")
except FileNotFoundError as e:
print(f"文件不存在: {e}")
except Exception as e:
print(f"发生其他异常: {e}")
main()
# 6. 异常处理的嵌套
print("\n异常处理的嵌套示例:")
try:
print("外层try")
try:
print("内层try")
10 / 0
except ZeroDivisionError as e:
print(f"内层except: {e}")
# 重新引发异常
raise
finally:
print("内层finally")
except Exception as e:
print(f"外层except: {e}")
finally:
print("外层finally")3. 异常的引发与传播
raise语句来引发异常,也可以捕获异常后重新引发异常。理解异常的引发与传播机制对于编写健壮的代码非常重要。3.1 raise语句
raise语句用于引发异常,基本语法如下:raise ExceptionType("异常信息")raise语句可以引发内置异常或自定义异常。当使用raise语句时,Python会立即停止执行当前代码,并开始查找匹配的except子句。3.2 重新引发异常
except子句中,我们可以使用不带参数的raise语句来重新引发当前捕获的异常。这通常用于记录异常信息后,将异常传递给上层调用者处理。3.3 异常的传播
3.4 异常链
raise NewException from OriginalException语句来创建异常链,这样可以保留原始异常的信息,便于调试。# 异常的引发与传播示例
# 1. 使用raise语句引发异常
print("使用raise语句引发异常示例:")
def divide(a, b):
if b == 0:
raise ZeroDivisionError("除数不能为零")
return a / b
try:
result = divide(10, 0)
except ZeroDivisionError as e:
print(f"捕获到异常: {e}")
# 2. 重新引发异常
print("\n重新引发异常示例:")
def process_data(data):
try:
if not data:
raise ValueError("数据不能为空")
return len(data)
except ValueError as e:
print(f"记录异常: {e}")
raise # 重新引发异常
try:
result = process_data("")
except ValueError as e:
print(f"捕获到重新引发的异常: {e}")
# 3. 异常的传播
print("\n异常的传播示例:")
def level1():
print("level1 开始")
level2()
print("level1 结束")
def level2():
print("level2 开始")
level3()
print("level2 结束")
def level3():
print("level3 开始")
raise ValueError("在level3中引发异常")
print("level3 结束")
try:
level1()
except ValueError as e:
print(f"捕获到异常: {e}")
# 4. 异常链
print("\n异常链示例:")
def read_config():
try:
with open("config.json", "r") as f:
# 假设这里需要解析JSON
raise ValueError("JSON格式错误")
except FileNotFoundError as e:
raise RuntimeError("无法读取配置文件") from e
try:
read_config()
except RuntimeError as e:
print(f"捕获到异常: {e}")
if e.__cause__:
print(f"原始异常: {e.__cause__}")
# 5. 自定义异常类
print("\n自定义异常类示例:")
class CustomError(Exception):
"""自定义异常类"""
def __init__(self, message, error_code):
super().__init__(message)
self.error_code = error_code
def __str__(self):
return f"{self.__class__.__name__}: {self.args[0]} (错误码: {self.error_code})"
def validate_input(value):
if value < 0:
raise CustomError("输入值不能为负数", 400)
return value
try:
result = validate_input(-5)
except CustomError as e:
print(f"捕获到自定义异常: {e}")
print(f"错误码: {e.error_code}")4. 自定义异常
4.1 创建自定义异常类
Exception类)__init__方法(可选)__str__方法(可选)4.2 自定义异常的最佳实践
4.3 自定义异常的应用场景
# 自定义异常示例
# 1. 基本的自定义异常类
print("基本的自定义异常类示例:")
class ValidationError(Exception):
"""验证错误异常"""
pass
def validate_email(email):
if '@' not in email:
raise ValidationError(f"无效的邮箱地址: {email}")
return email
try:
result = validate_email("invalid-email")
except ValidationError as e:
print(f"捕获到验证错误: {e}")
# 2. 带有自定义属性的异常类
print("\n带有自定义属性的异常类示例:")
class APIError(Exception):
"""API错误异常"""
def __init__(self, message, status_code, error_code):
super().__init__(message)
self.status_code = status_code
self.error_code = error_code
def __str__(self):
return f"APIError: {self.args[0]} (状态码: {self.status_code}, 错误码: {self.error_code})"
def call_api(endpoint):
if endpoint == "/error":
raise APIError("API调用失败", 500, "INTERNAL_SERVER_ERROR")
return "API调用成功"
try:
result = call_api("/error")
except APIError as e:
print(f"捕获到API错误: {e}")
print(f"状态码: {e.status_code}")
print(f"错误码: {e.error_code}")
# 3. 异常层次结构
print("\n异常层次结构示例:")
class AppError(Exception):
"""应用程序基础异常"""
pass
class DatabaseError(AppError):
"""数据库错误"""
pass
class NetworkError(AppError):
"""网络错误"""
pass
class ConnectionError(NetworkError):
"""连接错误"""
pass
class TimeoutError(NetworkError):
"""超时错误"""
pass
def connect_to_database():
raise DatabaseError("数据库连接失败")
def connect_to_api():
raise ConnectionError("API连接失败")
try:
connect_to_database()
except AppError as e:
print(f"捕获到应用程序错误: {e}")
try:
connect_to_api()
except NetworkError as e:
print(f"捕获到网络错误: {e}")
except AppError as e:
print(f"捕获到应用程序错误: {e}")
# 4. 自定义异常的实际应用
print("\n自定义异常的实际应用示例:")
class ConfigurationError(AppError):
"""配置错误"""
def __init__(self, message, config_key=None):
super().__init__(message)
self.config_key = config_key
class InputError(AppError):
"""输入错误"""
def __init__(self, message, input_value=None):
super().__init__(message)
self.input_value = input_value
def load_config(config):
if "database" not in config:
raise ConfigurationError("配置中缺少数据库信息", "database")
if "host" not in config["database"]:
raise ConfigurationError("配置中缺少数据库主机信息", "database.host")
return config
def process_input(value):
if not isinstance(value, int):
raise InputError("输入值必须是整数", value)
if value < 0:
raise InputError("输入值必须是非负数", value)
return value
try:
config = load_config({"api": {"key": "secret"}})
except ConfigurationError as e:
print(f"捕获到配置错误: {e}")
if e.config_key:
print(f"配置键: {e.config_key}")
try:
result = process_input(-5)
except InputError as e:
print(f"捕获到输入错误: {e}")
if e.input_value is not None:
print(f"输入值: {e.input_value}")5. 异常处理的最佳实践
5.1 异常处理的原则
finally子句或上下文管理器来确保资源的释放5.2 异常处理的常见错误
5.3 异常处理的模式
5.3.1 EAFP模式
try:
value = dictionary[key]
except KeyError:
value = default_value5.3.2 LBYL模式
if key in dictionary:
value = dictionary[key]
else:
value = default_value5.4 异常处理的性能考虑
# 异常处理的最佳实践示例
# 1. 只捕获必要的异常
print("只捕获必要的异常示例:")
try:
num = int(input("请输入一个整数: "))
result = 10 / num
print(f"结果: {result}")
except ValueError:
print("输入错误,请输入一个有效的整数")
except ZeroDivisionError:
print("错误:不能除以零")
# 不要这样做
# except Exception:
# print("发生错误")
# 2. 提供有意义的异常信息
print("\n提供有意义的异常信息示例:")
def divide(a, b):
if b == 0:
raise ZeroDivisionError(f"除数不能为零 (a={a}, b={b})")
return a / b
try:
result = divide(10, 0)
except ZeroDivisionError as e:
print(f"捕获到异常: {e}")
# 3. 使用finally子句释放资源
print("\n使用finally子句释放资源示例:")
def read_file(filename):
f = None
try:
f = open(filename, "r")
content = f.read()
return content
except FileNotFoundError:
print(f"文件不存在: {filename}")
return ""
finally:
if f is not None:
f.close()
print("文件已关闭")
content = read_file("non_existent_file.txt")
print(f"文件内容: {content}")
# 4. 使用上下文管理器
print("\n使用上下文管理器示例:")
def read_file_safely(filename):
try:
with open(filename, "r") as f:
content = f.read()
return content
except FileNotFoundError:
print(f"文件不存在: {filename}")
return ""
content = read_file_safely("non_existent_file.txt")
print(f"文件内容: {content}")
# 5. EAFP vs LBYL模式
print("\nEAFP vs LBYL模式示例:")
# EAFP模式
dictionary = {"a": 1, "b": 2}
key = "c"
try:
value = dictionary[key]
print(f"EAFP模式: 找到值: {value}")
except KeyError:
value = "默认值"
print(f"EAFP模式: 键不存在,使用默认值: {value}")
# LBYL模式
if key in dictionary:
value = dictionary[key]
print(f"LBYL模式: 找到值: {value}")
else:
value = "默认值"
print(f"LBYL模式: 键不存在,使用默认值: {value}")
# 6. 异常处理的性能考虑
print("\n异常处理的性能考虑示例:")
import time
# 测试正常情况下的性能
def test_normal_case():
start = time.time()
for i in range(1000000):
# 正常流程
pass
end = time.time()
print(f"正常情况耗时: {end - start:.4f}秒")
# 测试异常情况下的性能
def test_exception_case():
start = time.time()
for i in range(1000000):
try:
# 尝试执行操作
pass
except Exception:
# 捕获异常
pass
end = time.time()
print(f"异常处理耗时: {end - start:.4f}秒")
# 测试引发异常的性能
def test_raise_exception():
start = time.time()
for i in range(1000):
try:
raise Exception("测试异常")
except Exception:
pass
end = time.time()
print(f"引发异常耗时: {end - start:.4f}秒")
test_normal_case()
test_exception_case()
test_raise_exception()
# 7. 自定义异常的最佳实践
print("\n自定义异常的最佳实践示例:")
class BusinessLogicError(Exception):
"""业务逻辑错误"""
def __init__(self, message, error_code):
super().__init__(message)
self.error_code = error_code
def to_dict(self):
"""将异常转换为字典"""
return {
"error": self.__class__.__name__,
"message": self.args[0],
"error_code": self.error_code
}
def process_order(order):
if not order.get("customer_id"):
raise BusinessLogicError("订单缺少客户ID", "MISSING_CUSTOMER_ID")
if order.get("amount") <= 0:
raise BusinessLogicError("订单金额必须大于零", "INVALID_AMOUNT")
return "订单处理成功"
try:
order = {"amount": -100}
result = process_order(order)
print(f"结果: {result}")
except BusinessLogicError as e:
error_info = e.to_dict()
print(f"捕获到业务逻辑错误: {error_info}")6. 异常处理与日志记录
6.1 日志记录的基本概念
logging模块提供了一个灵活的日志记录系统。6.2 异常处理与日志记录的结合
logging.exception()函数记录异常的详细信息,包括堆栈跟踪DEBUG:详细的调试信息INFO:一般信息WARNING:警告信息ERROR:错误信息CRITICAL:严重错误信息6.3 日志记录的配置
logging模块提供了灵活的配置选项,我们可以根据需要配置日志记录的行为。以下是一些常见的配置选项:# 异常处理与日志记录示例
import logging
# 配置日志
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
filename='app.log',
filemode='a'
)
# 创建logger
logger = logging.getLogger(__name__)
# 1. 基本的异常日志记录
print("基本的异常日志记录示例:")
def divide(a, b):
try:
return a / b
except ZeroDivisionError as e:
logger.error(f"除以零错误: a={a}, b={b}", exc_info=True)
raise
try:
result = divide(10, 0)
except ZeroDivisionError as e:
print(f"捕获到异常: {e}")
# 2. 使用logging.exception()
print("\n使用logging.exception()示例:")
def read_config(filename):
try:
with open(filename, "r") as f:
content = f.read()
return content
except Exception as e:
logger.exception(f"读取配置文件失败: {filename}")
raise
try:
content = read_config("non_existent_config.json")
except Exception as e:
print(f"捕获到异常: {e}")
# 3. 不同级别的日志
print("\n不同级别的日志示例:")
def process_data(data):
if not data:
logger.warning("数据为空")
return []
try:
processed_data = [int(item) for item in data.split(',')]
logger.info(f"成功处理数据: {data}")
return processed_data
except ValueError as e:
logger.error(f"数据处理失败: {data}", exc_info=True)
raise
try:
result = process_data("")
print(f"结果: {result}")
except Exception as e:
print(f"捕获到异常: {e}")
try:
result = process_data("1,2,3")
print(f"结果: {result}")
except Exception as e:
print(f"捕获到异常: {e}")
try:
result = process_data("1,2,abc")
print(f"结果: {result}")
except Exception as e:
print(f"捕获到异常: {e}")
# 4. 日志配置示例
print("\n日志配置示例:")
# 更复杂的日志配置
import logging.config
logging_config = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'standard': {
'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
},
'detailed': {
'format': '%(asctime)s - %(name)s - %(levelname)s - %(module)s:%(lineno)d - %(message)s'
}
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'level': 'INFO',
'formatter': 'standard',
'stream': 'ext://sys.stdout'
},
'file': {
'class': 'logging.handlers.RotatingFileHandler',
'level': 'DEBUG',
'formatter': 'detailed',
'filename': 'app.log',
'maxBytes': 10485760, # 10MB
'backupCount': 5
}
},
'loggers': {
'': {
'handlers': ['console', 'file'],
'level': 'DEBUG',
'propagate': True
}
}
}
logging.config.dictConfig(logging_config)
# 测试配置后的日志
logger = logging.getLogger(__name__)
logger.debug("这是一条调试信息")
logger.info("这是一条一般信息")
logger.warning("这是一条警告信息")
logger.error("这是一条错误信息")
logger.critical("这是一条严重错误信息")
# 5. 异常处理与日志记录的最佳实践
print("\n异常处理与日志记录的最佳实践示例:")
def safe_operation(func):
"""安全操作装饰器"""
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
logger.exception(f"操作失败: {func.__name__}")
raise
return wrapper
@safe_operation
def risky_operation():
""" risky operation """
10 / 0
try:
risky_operation()
except Exception as e:
print(f"捕获到异常: {e}")
# 6. 自定义异常与日志记录
print("\n自定义异常与日志记录示例:")
class AppException(Exception):
"""应用程序异常"""
def __init__(self, message, error_code, level=logging.ERROR):
super().__init__(message)
self.error_code = error_code
self.level = level
def log(self, logger):
"""记录异常"""
if self.level == logging.ERROR:
logger.exception(f"{self.__class__.__name__}: {self.args[0]} (错误码: {self.error_code})")
else:
logger.log(self.level, f"{self.__class__.__name__}: {self.args[0]} (错误码: {self.error_code})")
class ValidationError(AppException):
"""验证错误"""
def __init__(self, message, field):
super().__init__(message, "VALIDATION_ERROR", logging.WARNING)
self.field = field
def log(self, logger):
logger.warning(f"ValidationError: {self.args[0]} (字段: {self.field})")
def validate_user(user):
if not user.get("name"):
raise ValidationError("用户名不能为空", "name")
if not user.get("email"):
raise ValidationError("邮箱不能为空", "email")
if "@" not in user.get("email", ""):
raise ValidationError("邮箱格式无效", "email")
return True
try:
user = {"name": "John"}
validate_user(user)
print("用户验证成功")
except AppException as e:
e.log(logger)
print(f"捕获到应用程序异常: {e}")7. 异常处理与测试
7.1 测试异常的基本方法
unittest模块或pytest框架来测试异常。以下是测试异常的基本方法:assertRaises:测试代码是否会引发特定类型的异常assertRaisesRegex:测试代码是否会引发特定类型的异常,并且异常信息匹配特定的正则表达式pytest.raises:在pytest中测试代码是否会引发特定类型的异常7.2 测试异常的最佳实践
7.3 异常处理的测试示例
unittest模块和pytest框架测试异常的示例:# 异常处理与测试示例
# 1. 使用unittest模块测试异常
print("使用unittest模块测试异常示例:")
import unittest
class Calculator:
def divide(self, a, b):
if b == 0:
raise ZeroDivisionError("除数不能为零")
return a / b
class TestCalculator(unittest.TestCase):
def setUp(self):
self.calculator = Calculator()
def test_divide_normal(self):
"""测试正常除法"""
result = self.calculator.divide(10, 2)
self.assertEqual(result, 5)
def test_divide_zero(self):
"""测试除以零"""
with self.assertRaises(ZeroDivisionError) as cm:
self.calculator.divide(10, 0)
self.assertIn("除数不能为零", str(cm.exception))
# 运行测试
if __name__ == '__main__':
unittest.main(argv=['first-arg-is-ignored'], exit=False)
# 2. 使用pytest测试异常
print("\n使用pytest测试异常示例:")
# 以下代码需要在pytest环境中运行
'''
def test_divide_normal():
calculator = Calculator()
result = calculator.divide(10, 2)
assert result == 5
def test_divide_zero():
calculator = Calculator()
with pytest.raises(ZeroDivisionError, match="除数不能为零"):
calculator.divide(10, 0)
'''
# 3. 测试自定义异常
print("\n测试自定义异常示例:")
class ValidationError(Exception):
"""验证错误"""
pass
def validate_email(email):
if '@' not in email:
raise ValidationError(f"无效的邮箱地址: {email}")
return email
class TestValidation(unittest.TestCase):
def test_validate_email_valid(self):
"""测试有效的邮箱地址"""
result = validate_email("test@example.com")
self.assertEqual(result, "test@example.com")
def test_validate_email_invalid(self):
"""测试无效的邮箱地址"""
with self.assertRaises(ValidationError) as cm:
validate_email("invalid-email")
self.assertIn("无效的邮箱地址", str(cm.exception))
# 运行测试
if __name__ == '__main__':
unittest.main(argv=['first-arg-is-ignored'], exit=False)
# 4. 测试异常处理代码
print("\n测试异常处理代码示例:")
def safe_divide(a, b):
try:
return a / b
except ZeroDivisionError:
return float('inf')
except TypeError:
return None
class TestSafeDivide(unittest.TestCase):
def test_safe_divide_normal(self):
"""测试正常除法"""
result = safe_divide(10, 2)
self.assertEqual(result, 5)
def test_safe_divide_zero(self):
"""测试除以零"""
result = safe_divide(10, 0)
self.assertEqual(result, float('inf'))
def test_safe_divide_type_error(self):
"""测试类型错误"""
result = safe_divide(10, "2")
self.assertIsNone(result)
# 运行测试
if __name__ == '__main__':
unittest.main(argv=['first-arg-is-ignored'], exit=False)
# 5. 测试异常的边界情况
print("\n测试异常的边界情况示例:")
def process_list(items):
if not isinstance(items, list):
raise TypeError("参数必须是列表")
if not items:
raise ValueError("列表不能为空")
return sum(items)
class TestProcessList(unittest.TestCase):
def test_process_list_normal(self):
"""测试正常情况"""
result = process_list([1, 2, 3])
self.assertEqual(result, 6)
def test_process_list_not_list(self):
"""测试参数不是列表"""
with self.assertRaises(TypeError) as cm:
process_list("not a list")
self.assertIn("参数必须是列表", str(cm.exception))
def test_process_list_empty(self):
"""测试空列表"""
with self.assertRaises(ValueError) as cm:
process_list([])
self.assertIn("列表不能为空", str(cm.exception))
# 运行测试
if __name__ == '__main__':
unittest.main(argv=['first-arg-is-ignored'], exit=False)
# 6. 使用mock测试异常
print("\n使用mock测试异常示例:")
from unittest.mock import Mock, patch
def get_data_from_api(url):
import requests
try:
response = requests.get(url)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
raise RuntimeError(f"API调用失败: {e}")
class TestGetDataFromApi(unittest.TestCase):
@patch('requests.get')
def test_get_data_success(self, mock_get):
"""测试API调用成功"""
mock_response = Mock()
mock_response.json.return_value = {"data": "test"}
mock_response.raise_for_status.return_value = None
mock_get.return_value = mock_response
result = get_data_from_api("https://example.com/api")
self.assertEqual(result, {"data": "test"})
@patch('requests.get')
def test_get_data_failure(self, mock_get):
"""测试API调用失败"""
mock_response = Mock()
mock_response.raise_for_status.side_effect = Exception("API错误")
mock_get.return_value = mock_response
with self.assertRaises(RuntimeError) as cm:
get_data_from_api("https://example.com/api")
self.assertIn("API调用失败", str(cm.exception))
# 运行测试
if __name__ == '__main__':
unittest.main(argv=['first-arg-is-ignored'], exit=False)7. 异常处理的高级技巧
7.1 使用装饰器处理异常
7.2 使用上下文管理器处理异常
7.3 使用
contextlib.suppresscontextlib.suppress是Python 3.4+中引入的一个工具,它可以用于忽略特定类型的异常。7.4 使用
traceback模块traceback模块提供了一些函数,用于处理和格式化异常的堆栈跟踪信息。7.5 异常处理的性能优化
# 异常处理的高级技巧示例
# 1. 使用装饰器处理异常
print("使用装饰器处理异常示例:")
import functools
def handle_exceptions(default=None, log=True):
"""异常处理装饰器"""
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
if log:
print(f"函数 {func.__name__} 发生异常: {e}")
return default
return wrapper
return decorator
@handle_exceptions(default="错误", log=True)
def risky_operation():
""" risky operation """
10 / 0
result = risky_operation()
print(f"结果: {result}")
@handle_exceptions(default=[], log=False)
def parse_json(json_str):
"""解析JSON字符串"""
import json
return json.loads(json_str)
result = parse_json("invalid json")
print(f"解析结果: {result}")
# 2. 使用上下文管理器处理异常
print("\n使用上下文管理器处理异常示例:")
class ExceptionHandler:
"""异常处理上下文管理器"""
def __init__(self, default=None, *exceptions):
self.default = default
self.exceptions = exceptions or (Exception,)
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_type is not None and issubclass(exc_type, self.exceptions):
print(f"捕获到异常: {exc_val}")
return True # 抑制异常
return False # 不抑制异常
with ExceptionHandler(default="错误", ZeroDivisionError, ValueError) as handler:
result = 10 / 0
print(f"结果: {result}")
print("上下文管理器执行完毕")
# 3. 使用contextlib.suppress
print("\n使用contextlib.suppress示例:")
from contextlib import suppress
# 忽略特定异常
with suppress(ZeroDivisionError):
result = 10 / 0
print(f"结果: {result}")
print("操作完成")
# 忽略多个异常
with suppress(ZeroDivisionError, ValueError):
result = int("abc")
print(f"结果: {result}")
print("操作完成")
# 4. 使用traceback模块
print("\n使用traceback模块示例:")
import traceback
def nested_function():
"""嵌套函数"""
10 / 0
def outer_function():
"""外部函数"""
nested_function()
try:
outer_function()
except Exception as e:
print(f"捕获到异常: {e}")
print("\n堆栈跟踪:")
traceback.print_exc()
# 获取堆栈跟踪信息作为字符串
traceback_str = traceback.format_exc()
print("\n堆栈跟踪字符串:")
print(traceback_str)
# 5. 异常处理的性能优化
print("\n异常处理的性能优化示例:")
import time
# 测试EAFP模式的性能
def eafp_approach(dictionary, key):
"""使用EAFP模式"""
try:
return dictionary[key]
except KeyError:
return "默认值"
# 测试LBYL模式的性能
def lbyl_approach(dictionary, key):
"""使用LBYL模式"""
if key in dictionary:
return dictionary[key]
else:
return "默认值"
# 测试性能
dictionary = {f"key{i}": i for i in range(1000)}
# 测试键存在的情况
print("测试键存在的情况:")
start = time.time()
for i in range(1000000):
eafp_approach(dictionary, "key500")
end = time.time()
print(f"EAFP模式耗时: {end - start:.4f}秒")
start = time.time()
for i in range(1000000):
lbyl_approach(dictionary, "key500")
end = time.time()
print(f"LBYL模式耗时: {end - start:.4f}秒")
# 测试键不存在的情况
print("\n测试键不存在的情况:")
start = time.time()
for i in range(100000):
eafp_approach(dictionary, "key1000")
end = time.time()
print(f"EAFP模式耗时: {end - start:.4f}秒")
start = time.time()
for i in range(100000):
lbyl_approach(dictionary, "key1000")
end = time.time()
print(f"LBYL模式耗时: {end - start:.4f}秒")
# 6. 使用functools.wraps保留函数元数据
print("\n使用functools.wraps保留函数元数据示例:")
def error_handler(func):
"""错误处理装饰器"""
@functools.wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
print(f"错误: {e}")
raise
return wrapper
@error_handler
def calculate(a, b):
"""计算两个数的和"""
return a + b
print(f"函数名: {calculate.__name__}")
print(f"函数文档: {calculate.__doc__}")
print(f"函数参数: {calculate.__code__.co_varnames}")
try:
result = calculate(10, "20")
print(f"结果: {result}")
except Exception as e:
print(f"捕获到异常: {e}")8. 常见异常处理场景
8.1 文件操作
8.2 网络操作
8.3 数据库操作
8.4 API调用
8.5 输入验证
# 常见异常处理场景示例
# 1. 文件操作
print("文件操作示例:")
def read_file_safely(filename):
"""安全地读取文件"""
try:
with open(filename, "r", encoding="utf-8") as f:
content = f.read()
return content
except FileNotFoundError:
print(f"错误:文件 {filename} 不存在")
return ""
except PermissionError:
print(f"错误:没有读取文件 {filename} 的权限")
return ""
except UnicodeDecodeError:
print(f"错误:文件 {filename} 编码错误")
return ""
except Exception as e:
print(f"错误:读取文件时发生未知错误: {e}")
return ""
content = read_file_safely("non_existent_file.txt")
print(f"文件内容长度: {len(content)}")
# 2. 网络操作
print("\n网络操作示例:")
def fetch_url(url, timeout=10):
"""获取URL内容"""
import requests
try:
response = requests.get(url, timeout=timeout)
response.raise_for_status() # 引发HTTP错误
return response.text
except requests.exceptions.ConnectionError:
print(f"错误:无法连接到 {url}")
return ""
except requests.exceptions.Timeout:
print(f"错误:请求 {url} 超时")
return ""
except requests.exceptions.HTTPError as e:
print(f"错误:HTTP错误: {e}")
return ""
except Exception as e:
print(f"错误:发生未知错误: {e}")
return ""
# 测试网络操作
# content = fetch_url("https://example.com")
# print(f"URL内容长度: {len(content)}")
# 3. 数据库操作
print("\n数据库操作示例:")
def query_database(query, params=None):
"""查询数据库"""
import sqlite3
try:
conn = sqlite3.connect(":memory:")
cursor = conn.cursor()
cursor.execute(query, params or ())
results = cursor.fetchall()
conn.commit()
return results
except sqlite3.Error as e:
print(f"数据库错误: {e}")
return []
finally:
if 'conn' in locals():
conn.close()
# 测试数据库操作
results = query_database("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)")
print(f"创建表结果: {results}")
results = query_database("INSERT INTO users (name) VALUES (?)", ("John",))
print(f"插入结果: {results}")
results = query_database("SELECT * FROM users")
print(f"查询结果: {results}")
# 4. API调用
print("\nAPI调用示例:")
class APIError(Exception):
"""API错误"""
pass
def call_api(endpoint, method="GET", data=None):
"""调用API"""
import requests
base_url = "https://api.example.com"
url = f"{base_url}{endpoint}"
try:
if method == "GET":
response = requests.get(url, params=data)
elif method == "POST":
response = requests.post(url, json=data)
else:
raise APIError(f"不支持的HTTP方法: {method}")
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
raise APIError(f"网络错误: {e}")
except ValueError:
raise APIError("API返回的不是有效的JSON")
except Exception as e:
raise APIError(f"未知错误: {e}")
# 测试API调用
# try:
# result = call_api("/users", method="GET", data={"page": 1})
# print(f"API调用结果: {result}")
# except APIError as e:
# print(f"捕获到API错误: {e}")
# 5. 输入验证
print("\n输入验证示例:")
class ValidationError(Exception):
"""验证错误"""
pass
def validate_input(data):
"""验证输入数据"""
if not isinstance(data, dict):
raise ValidationError("输入必须是字典")
required_fields = ["name", "email", "age"]
for field in required_fields:
if field not in data:
raise ValidationError(f"缺少必填字段: {field}")
if not isinstance(data["name"], str) or not data["name"]:
raise ValidationError("姓名必须是非空字符串")
if not isinstance(data["email"], str) or "@" not in data["email"]:
raise ValidationError("邮箱格式无效")
if not isinstance(data["age"], int) or data["age"] < 0:
raise ValidationError("年龄必须是非负整数")
return True
try:
user_data = {
"name": "John",
"email": "john@example.com",
"age": 30
}
validate_input(user_data)
print("输入验证成功")
except ValidationError as e:
print(f"验证错误: {e}")
try:
user_data = {
"name": "",
"email": "invalid-email",
"age": -5
}
validate_input(user_data)
print("输入验证成功")
except ValidationError as e:
print(f"验证错误: {e}")9. 总结
10. 参考文献
11. 结语