Python 3 - chức năng và oops 9 xử lý ngoại lệ 4

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. 
0

Python 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. 
0

try:
    # 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. 
5

Khi 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 try

Ví 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ỏ qua

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 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 thi

Nế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ỏ qua

Nắ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 nhau

Loạ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. 
1

Trong 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 ra

Khi 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ệ
    try:
        numerator = 10
        denominator = 0
    
        result = numerator/denominator
    
        print[result]
    except:
        print["Error: Denominator cannot be 0."]
    
    # Output: Error: Denominator cannot be 0. 
    22 bị bỏ qua
  • Bộ mã bên trong 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 được thực thi

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. 
3

Hã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
2

Nế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
3

Tuy 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
7

Ghi 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 except

Python 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ông

Khố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
11

Hã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. 
4

Trong 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ệ

Chủ Đề