Trong hướng dẫn trước, chúng ta đã tìm hiểu về ngoại lệ Python. Chúng tôi biết rằng các ngoại lệ chấm dứt việc thực hiện chương trình một cách bất thường
Đây là lý do tại sao điều quan trọng là phải xử lý các ngoại lệ. Trong Python, chúng tôi sử dụng khối
try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
0Python thử. ngoại trừ khối
Khối
try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
0 được sử dụng để xử lý các ngoại lệ trong Python. Đây là cú pháp của khối try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
0try:
# code that may cause exception
except:
# code to run when exception occurs
Ở đây, chúng tôi đã đặt mã có thể tạo ngoại lệ bên trong khối
try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
3. Mỗi khối try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
3 được theo sau bởi một khối try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
5Khi một ngoại lệ xảy ra, nó sẽ bị chặn bởi khối
try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
5. Không thể sử dụng khối try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
5 nếu không có khối tryVí dụ. Xử lý ngoại lệ Sử dụng thử. ngoại trừ
try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
Trong ví dụ này, chúng tôi đang cố gắng chia một số cho 0. Ở đây, mã này tạo ra một ngoại lệ
Để xử lý ngoại lệ, chúng tôi đã đặt mã,
try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
8 bên trong khối try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
3. Bây giờ khi một ngoại lệ xảy ra, phần còn lại của mã bên trong khối try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
3 sẽ bị bỏ quaKhối
try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
5 bắt ngoại lệ và các câu lệnh bên trong khối try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
5 được thực thiNếu không có câu lệnh nào trong khối
try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
3 tạo ra ngoại lệ, thì khối try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
5 sẽ bị bỏ quaNắm bắt các ngoại lệ cụ thể trong Python
Đối với mỗi khối
try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
3, có thể có 0 hoặc nhiều khối try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
5. Nhiều khối try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
5 cho phép chúng tôi xử lý từng ngoại lệ khác nhauLoại đối số của mỗi khối
try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
5 cho biết loại ngoại lệ mà nó có thể xử lý. Ví dụ,try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
1Trong ví dụ này, chúng tôi đã tạo một danh sách có tên là số chẵn
Vì chỉ số danh sách bắt đầu từ 0 nên phần tử cuối cùng của danh sách nằm ở chỉ số 3. Chú ý tuyên bố,
try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
2Ở đây, chúng tôi đang cố gắng truy cập một giá trị cho chỉ mục 5. Do đó, ngoại lệ
try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
19 xảy raKhi ngoại lệ
try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
19 xảy ra trong khối try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
3,- Ngoại lệ
22 bị bỏ quatry: numerator = 10 denominator = 0 result = numerator/denominator print[result] except: print["Error: Denominator cannot be 0."] # Output: Error: Denominator cannot be 0.
- Bộ mã bên trong ngoại lệ
19 được thực thitry: numerator = 10 denominator = 0 result = numerator/denominator print[result] except: print["Error: Denominator cannot be 0."] # Output: Error: Denominator cannot be 0.
Python thử với mệnh đề khác
Trong một số trường hợp, chúng tôi có thể muốn chạy một khối mã nhất định nếu khối mã bên trong
try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
3 chạy mà không có bất kỳ lỗi nàoĐối với những trường hợp này, bạn có thể sử dụng từ khóa
try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
25 tùy chọn với câu lệnh try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
3Hãy xem một ví dụ
try:
# code that may cause exception
except:
# code to run when exception occurs
1đầu ra
Nếu chúng ta vượt qua một số lẻ
try:
# code that may cause exception
except:
# code to run when exception occurs
2Nếu chúng ta vượt qua một số chẵn, đối ứng được tính toán và hiển thị
try:
# code that may cause exception
except:
# code to run when exception occurs
3Tuy nhiên, nếu chúng tôi vượt qua 0, chúng tôi nhận được
try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
22 vì khối mã bên trong try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
25 không được xử lý bởi try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
5 trước đótry:
# code that may cause exception
except:
# code to run when exception occurs
7Ghi chú. Các ngoại lệ trong mệnh đề
try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
25 không được xử lý bởi các mệnh đề trước exceptPython thử. cuối cùng
Trong Python, khối
try:
# code that may cause exception
except:
# code to run when exception occurs
11 luôn được thực thi bất kể có ngoại lệ hay khôngKhối
try:
# code that may cause exception
except:
# code to run when exception occurs
11 là tùy chọn. Và, đối với mỗi khối try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
3, chỉ có thể có một khối try:
# code that may cause exception
except:
# code to run when exception occurs
11Hãy xem một ví dụ,
try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
3đầu ra
try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
4Trong ví dụ trên, chúng tôi đang chia một số cho 0 bên trong khối
try:
numerator = 10
denominator = 0
result = numerator/denominator
print[result]
except:
print["Error: Denominator cannot be 0."]
# Output: Error: Denominator cannot be 0.
3. Ở đây, mã này tạo ra một ngoại lệ