Python異常處理操作舉例詳細教程
更新時間:2025年05月27日 10:47:18 作者:Yant224
異常處理的能力是一門語言好壞的重要特征,python也提供了強大的異常處理功能,這篇文章主要介紹了Python異常處理操作的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下

一、異常處理基礎(chǔ)
1. 基本語法結(jié)構(gòu)
try:
# 可能引發(fā)異常的代碼
result = 10 / 0
except ZeroDivisionError:
# 處理特定異常
print("不能除以零!")
except Exception as e:
# 通用異常處理
print(f"發(fā)生錯誤: {e}")
else:
# 無異常時執(zhí)行
print("計算成功")
finally:
# 始終執(zhí)行的清理代碼
print("清理資源")
- 異常傳播機制

二、常見異常類型
1. 內(nèi)置異常層次
所有異常類都繼承 BaseException
BaseException
├── KeyboardInterrupt
├── SystemExit
└── Exception
├── ArithmeticError
│ ├── ZeroDivisionError
│ └── FloatingPointError
├── LookupError
│ ├── IndexError
│ └── KeyError
├── OSError
│ ├── FileNotFoundError
│ └── PermissionError
└── ValueError
2. 常見異常示例
# 文件操作
try:
with open("nonexist.txt") as f:
content = f.read()
except FileNotFoundError:
print("文件不存在")
# 類型轉(zhuǎn)換
try:
num = int("abc")
except ValueError:
print("無效的數(shù)字格式")
# 字典操作
d = {"key": "value"}
try:
print(d["missing"])
except KeyError:
print("鍵不存在")
三、多重異常處理
1. 合并捕獲
try:
# 可能拋出多種異常的代碼
except (TypeError, ValueError) as e:
print(f"輸入類型錯誤: {e}")
2. 分層處理
try:
data = json.loads(invalid_json)
except json.JSONDecodeError:
print("JSON解析失敗")
except Exception:
print("其他未知錯誤")
四、finally與else子句
1. finally 應用場景
file = None
try:
file = open("data.txt", "r")
process(file)
except IOError:
print("文件操作錯誤")
finally:
if file:
file.close() # 確保資源釋放
2. else 使用技巧
try:
result = risky_operation()
except NetworkError:
handle_error()
else:
save_result(result) # 僅在成功時執(zhí)行
五、自定義異常
1. 創(chuàng)建異常類
class InvalidEmailError(ValueError):
"""自定義郵箱格式異常"""
def __init__(self, email):
super().__init__(f"無效郵箱格式: {email}")
self.email = email
# 使用示例
email = "user@"
if "@" not in email or "." not in email.split("@")[1]:
raise InvalidEmailError(email)
2. 異常繼承體系
class DatabaseError(Exception):
"""數(shù)據(jù)庫操作基類異常"""
class ConnectionError(DatabaseError):
"""數(shù)據(jù)庫連接異常"""
class QueryError(DatabaseError):
"""SQL查詢異常"""
六、異常鏈與上下文
1. raise from 語法
try:
config = load_config()
except FileNotFoundError as e:
raise RuntimeError("配置加載失敗") from e
2. 異常上下文查看
try:
# 可能出錯的代碼
except Exception as e:
print("原始異常:", e.__cause__)
print("上下文:", e.__context__)
七、最佳實踐與陷阱
? 推薦做法
- 具體異常捕獲優(yōu)先
- 添加有意義的錯誤信息
raise ValueError(f"無效參數(shù)值: {value}")
- 日志記錄異常
import logging
try:
process()
except Exception:
logging.exception("處理失敗")
?? 避免陷阱
- 捕獲過于寬泛的異常
try:
...
except: # 捕獲所有異常,包括KeyboardInterrupt
pass
- 忽略異常
try:
...
except Exception:
pass # 靜默失敗
- 重復拋出異常
try:
...
except Exception as e:
print(e)
raise # 保留原始堆棧信息
綜合實戰(zhàn)案例
網(wǎng)絡(luò)請求重試機制
import requests
from time import sleep
def fetch_data(url, retries=3):
for attempt in range(retries):
try:
response = requests.get(url, timeout=5)
response.raise_for_status()
return response.json()
except requests.HTTPError as e:
print(f"HTTP錯誤: {e}")
except requests.Timeout:
print("請求超時")
except requests.RequestException:
print("網(wǎng)絡(luò)連接問題")
if attempt < retries - 1:
sleep(2 ** attempt) # 指數(shù)退避
raise Exception(f"請求失敗,已重試{retries}次")
# 使用示例
data = fetch_data("https://api.example.com/data")
通過掌握這些異常處理技巧,您可以:
- 編寫更健壯的程序
- 提高錯誤診斷效率
- 創(chuàng)建更友好的用戶交互體驗
- 構(gòu)建可維護的異常處理體系
建議在實際開發(fā)中結(jié)合具體業(yè)務場景,設(shè)計合理的異常處理策略,并定期審查異常處理代碼的有效性
總結(jié)
到此這篇關(guān)于Python異常處理操作的文章就介紹到這了,更多相關(guān)Python異常處理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python進階多線程爬取網(wǎng)頁項目實戰(zhàn)
這篇文章主要為大家介紹了Python進階,Python多線程爬取網(wǎng)頁項目實戰(zhàn)的示例呈現(xiàn)步驟,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10

