# 處理錯誤
> 原文: [https://thepythonguru.com/handling-errors/](https://thepythonguru.com/handling-errors/)
* * *
于 2020 年 1 月 7 日更新
* * *
與數據庫交互是一個容易出錯的過程,因此我們必須始終實現某種機制來優雅地處理錯誤。
MySQLdb 具有`MySQLdb.Error`異常,這是一個頂級異常,可用于捕獲`MySQLdb`模塊引發的所有數據庫異常。
```py
from __future__ import print_function
import MySQLdb as my
try:
db = my.connect(host="127.0.0.1",
user="root",
passwd="",
db="world"
)
cursor = db.cursor()
sql = "select * from city"
number_of_rows = cursor.execute(sql)
print(number_of_rows)
db.close()
except my.Error as e:
print(e)
except :
print("Unknown error occurred")
```
## MySQLdb 中的兩個主要錯誤
* * *
需要注意的是,MySQLdb 中有兩類異常類:
1. `DatabaseError`
2. `InterfaceError`
* * *
1. **`DatabaseError`**:當數據處理中存在問題,sql 語法錯誤,mysql 內部問題時,引發此異常。 如果建立連接并且出現問題,則`DatabaseError`會捕獲到它。
2. **`InterfaceError`**:當由于某種原因數據庫連接失敗時,MySQLdb 將引發`InterfaceError`。 注意`InterfaceError`僅在與數據庫連接存在內部問題時才引發,MySQLdb 不會因錯誤的數據庫名稱或密碼而引發`InterfaceError`。
`DatabaseError`進一步分為 6 種類型:
1. `DataError`
2. `InternalError`
3. `IntegrityError`
4. `OperationalError`
5. `NotSupportedError`
6. `ProgrammingError`
* * *
1. **`DataError`**:當數據處理出現問題時,例如除以零,范圍的數值,MySQLdb 會引發此錯誤。
2. **`InternalError`**:當 MySQL 數據庫本身存在一些內部錯誤時,引發此異常。 例如無效的游標,事務不同步等。
3. **`IntegrityError`**:當外鍵檢查失敗時,引發此異常。
4. **`OperationalError`**:對于不受程序員控制的事情,會引發此異常。 例如,意外斷開連接,內存分配錯誤等,所選數據庫不存在。
5. **`NotSupportedError`**:當存在不支持的方法或 api 時,引發此異常。
6. **`ProgrammingError`**:引發此編程錯誤。 例如找不到表,mysql 語法錯誤,指定的參數數量錯誤等。
```py
from __future__ import print_function
import MySQLdb as my
try:
db = my.connect(host="127.0.0.1",
user="root",
passwd="",
db="world"
)
cursor = db.cursor()
sql = "select * from city"
number_of_rows = cursor.execute(sql)
print(number_of_rows)
db.close()
except my.DataError as e:
print("DataError")
print(e)
except my.InternalError as e:
print("InternalError")
print(e)
except my.IntegrityError as e:
print("IntegrityError")
print(e)
except my.OperationalError as e:
print("OperationalError")
print(e)
except my.NotSupportedError as e:
print("NotSupportedError")
print(e)
except my.ProgrammingError as e:
print("ProgrammingError")
print(e)
except :
print("Unknown error occurred")
```
在下一篇文章中,我們討論[如何從數據庫](/fetching-records-using-fetchone-and-fetchmany/)中獲取特定的行數。
* * *
* * *
- 初級 Python
- python 入門
- 安裝 Python3
- 運行 python 程序
- 數據類型和變量
- Python 數字
- Python 字符串
- Python 列表
- Python 字典
- Python 元組
- 數據類型轉換
- Python 控制語句
- Python 函數
- Python 循環
- Python 數學函數
- Python 生成隨機數
- Python 文件處理
- Python 對象和類
- Python 運算符重載
- Python 繼承與多態
- Python 異常處理
- Python 模塊
- 高級 Python
- Python *args和**kwargs
- Python 生成器
- Python 正則表達式
- 使用 PIP 在 python 中安裝包
- Python virtualenv指南
- Python 遞歸函數
- __name__ == "__main__"是什么?
- Python Lambda 函數
- Python 字符串格式化
- Python 內置函數和方法
- Python abs()函數
- Python bin()函數
- Python id()函數
- Python map()函數
- Python zip()函數
- Python filter()函數
- Python reduce()函數
- Python sorted()函數
- Python enumerate()函數
- Python reversed()函數
- Python range()函數
- Python sum()函數
- Python max()函數
- Python min()函數
- Python eval()函數
- Python len()函數
- Python ord()函數
- Python chr()函數
- Python any()函數
- Python all()函數
- Python globals()函數
- Python locals()函數
- 數據庫訪問
- 安裝 Python MySQLdb
- 連接到數據庫
- MySQLdb 獲取結果
- 插入行
- 處理錯誤
- 使用fetchone()和fetchmany()獲取記錄
- 常見做法
- Python:如何讀取和寫入文件
- Python:如何讀取和寫入 CSV 文件
- 用 Python 讀寫 JSON
- 用 Python 轉儲對象