Python xử lý oserror như thế nào?

Python in truy nguyên khi có ngoại lệ trong mã của bạn. Đầu ra theo dõi có thể hơi choáng ngợp nếu bạn nhìn thấy nó lần đầu tiên hoặc bạn không biết nó đang nói gì với bạn. Tuy nhiên, truy nguyên Python có rất nhiều thông tin có thể giúp bạn chẩn đoán và khắc phục lý do ngoại lệ được đưa ra trong mã của bạn. Hiểu những thông tin mà truy nguyên Python cung cấp là rất quan trọng để trở thành một lập trình viên Python giỏi hơn

Đến cuối hướng dẫn này, bạn sẽ có thể

  • Ý nghĩa của truy nguyên tiếp theo mà bạn nhìn thấy
  • Nhận biết một số dấu vết phổ biến hơn
  • Đăng nhập truy nguyên thành công trong khi vẫn xử lý ngoại lệ

Tiền thưởng miễn phí. Nhấp vào đây để nhận Bảng cheat Python miễn phí của chúng tôi, trang này cho bạn biết kiến ​​thức cơ bản về Python 3, như làm việc với các kiểu dữ liệu, từ điển, danh sách và hàm Python

Truy nguyên Python là gì?

Truy nguyên là một báo cáo chứa các lệnh gọi hàm được thực hiện trong mã của bạn tại một điểm cụ thể. Truy nguyên được biết đến với nhiều tên, bao gồm theo dõi ngăn xếp, truy nguyên ngăn xếp, truy nguyên ngược và có thể các tên khác. Trong Python, thuật ngữ được sử dụng là truy nguyên

Khi chương trình của bạn dẫn đến một ngoại lệ, Python sẽ in truy nguyên hiện tại để giúp bạn biết điều gì đã xảy ra. Dưới đây là một ví dụ để minh họa tình huống này

# example.py
def greet[someone]:
    print['Hello, ' + someon]

greet['Chad']

Ở đây,

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8 được gọi với tham số
>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
9. Tuy nhiên, trong
>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8, tên biến đó không được sử dụng. Thay vào đó, nó đã bị viết sai chính tả thành
# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
1 trong lệnh gọi
# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
2

Ghi chú. Hướng dẫn này giả định rằng bạn hiểu các ngoại lệ của Python. Nếu bạn chưa quen hoặc chỉ muốn ôn lại, thì bạn nên xem Python Exceptions. Một lời giới thiệu

Khi bạn chạy chương trình này, bạn sẽ nhận được dấu vết sau

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined

Đầu ra truy nguyên này có tất cả thông tin bạn cần để chẩn đoán sự cố. Dòng cuối cùng của đầu ra truy nguyên cho bạn biết loại ngoại lệ nào đã được nêu ra cùng với một số thông tin liên quan về ngoại lệ đó. Các dòng trước đó của truy nguyên chỉ ra mã dẫn đến ngoại lệ được nêu ra

Trong truy ngược ở trên, ngoại lệ là một

# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
3, có nghĩa là có một tham chiếu đến một số tên [biến, hàm, lớp] chưa được xác định. Trong trường hợp này, tên được tham chiếu là
# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
1

Dòng cuối cùng trong trường hợp này có đủ thông tin để giúp bạn khắc phục sự cố. Tìm kiếm mã cho tên

# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
1, một lỗi chính tả, sẽ chỉ cho bạn đúng hướng. Tuy nhiên, thường thì mã của bạn phức tạp hơn rất nhiều

Loại bỏ các quảng cáo

Làm thế nào để bạn đọc một Traceback Python?

Truy nguyên Python chứa rất nhiều thông tin hữu ích khi bạn đang cố gắng xác định lý do cho một ngoại lệ được đưa ra trong mã của bạn. Trong phần này, bạn sẽ xem qua các lần truy nguyên khác nhau để hiểu các phần thông tin khác nhau có trong một lần truy nguyên

Tổng quan về truy nguyên Python

Có một số phần quan trọng đối với mọi truy nguyên Python. Sơ đồ dưới đây làm nổi bật các phần khác nhau

Trong Python, tốt nhất là đọc truy nguyên từ dưới lên

  1. Hộp màu xanh. Dòng cuối cùng của truy nguyên là dòng thông báo lỗi. Nó chứa tên ngoại lệ đã được nêu ra

  2. Chiêc hộp xanh. Sau tên ngoại lệ là thông báo lỗi. Thông báo này thường chứa thông tin hữu ích để hiểu lý do ngoại lệ được đưa ra

  3. hộp màu vàng. Tiếp tục truy nguyên là các cuộc gọi chức năng khác nhau di chuyển từ dưới lên trên, gần đây nhất đến gần đây nhất. Các cuộc gọi này được thể hiện bằng các mục nhập hai dòng cho mỗi cuộc gọi. Dòng đầu tiên của mỗi lệnh gọi chứa thông tin như tên tệp, số dòng và tên mô-đun, tất cả chỉ định nơi có thể tìm thấy mã

  4. gạch chân màu đỏ. Dòng thứ hai cho các cuộc gọi này chứa mã thực tế đã được thực thi

Có một số khác biệt giữa đầu ra truy nguyên khi bạn đang thực thi mã của mình trong dòng lệnh và chạy mã trong REPL. Dưới đây là mã tương tự từ phần trước được thực thi trong REPL và kết quả là đầu ra truy nguyên

>>>

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined

Lưu ý rằng thay cho tên tệp, bạn nhận được

# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
6. Điều này hợp lý vì bạn đã nhập mã thông qua đầu vào tiêu chuẩn. Ngoài ra, các dòng mã đã thực hiện không được hiển thị trong truy nguyên

Ghi chú. Nếu bạn đã quen nhìn thấy dấu vết ngăn xếp trong các ngôn ngữ lập trình khác, thì bạn sẽ nhận thấy sự khác biệt lớn trong cách so sánh với dấu vết của Python. Hầu hết các ngôn ngữ khác in ngoại lệ ở trên cùng và sau đó đi từ trên xuống dưới, cuộc gọi gần đây nhất đến cuộc gọi gần đây nhất

Nó đã được nói rồi, nhưng chỉ để nhắc lại, truy nguyên Python nên được đọc từ dưới lên trên. Điều này rất hữu ích vì truy nguyên được in ra và thiết bị đầu cuối của bạn [hoặc bất cứ nơi nào bạn đang đọc truy nguyên] thường kết thúc ở cuối đầu ra, giúp bạn có một vị trí hoàn hảo để bắt đầu đọc truy nguyên

Hướng dẫn truy nguyên cụ thể

Xem qua một số đầu ra truy ngược cụ thể sẽ giúp bạn hiểu rõ hơn và xem truy nguyên sẽ cung cấp cho bạn thông tin gì

Đoạn mã dưới đây được sử dụng trong các ví dụ sau để minh họa thông tin mà truy nguyên Python cung cấp cho bạn

# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]

Ở đây,

# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
7 lấy một giá trị,
# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
8, và trả về giá trị đó hoặc nhắc trả về một giá trị thay thế

Sau đó,

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8 lấy một cái tên để được chào đón,
>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
9, và một giá trị
>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
71 tùy chọn và gọi
# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
2.
# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
7 cũng được gọi với giá trị
>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
9 được truyền vào

Cuối cùng,

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
75 sẽ lặp qua danh sách của
>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
76 và gọi
>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8. Nếu có một ngoại lệ được đưa ra bằng cách gọi
>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8, thì một lời chào dự phòng đơn giản sẽ được in

Mã này không có bất kỳ lỗi nào dẫn đến ngoại lệ được đưa ra miễn là đầu vào phù hợp được cung cấp

Nếu bạn thêm lệnh gọi tới

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8 vào cuối
>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
60 và chỉ định một đối số từ khóa mà nó không mong đợi [ví dụ:
>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
61], thì bạn sẽ nhận được truy nguyên sau

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
7

Một lần nữa, với truy nguyên Python, tốt nhất là làm việc ngược lại, di chuyển đầu ra lên. Bắt đầu từ dòng cuối cùng của truy nguyên, bạn có thể thấy rằng ngoại lệ là một

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
62. Các thông báo theo loại ngoại lệ, mọi thứ sau dấu hai chấm, cung cấp cho bạn một số thông tin tuyệt vời. Nó cho bạn biết rằng
>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8 đã được gọi với một đối số từ khóa mà nó không mong đợi. Tên đối số chưa biết cũng được cung cấp cho bạn.
>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
64

Di chuyển lên trên, bạn có thể thấy dòng dẫn đến ngoại lệ. Trong trường hợp này, đó là cuộc gọi

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8 mà chúng tôi đã thêm vào cuối của
>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
60

Dòng tiếp theo cung cấp cho bạn đường dẫn đến tệp chứa mã, số dòng của tệp đó nơi có thể tìm thấy mã và mã đó nằm trong mô-đun nào. Trong trường hợp này, vì mã của chúng tôi không sử dụng bất kỳ mô-đun Python nào khác, chúng tôi chỉ thấy

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
67 ở đây, nghĩa là đây là tệp đang được thực thi

Với một tệp khác và đầu vào khác, bạn có thể thấy quá trình truy nguyên thực sự chỉ cho bạn đúng hướng để tìm ra sự cố. Nếu bạn đang theo dõi, hãy xóa lệnh gọi

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8 có lỗi ở cuối
>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
60 và thêm tệp sau vào thư mục của bạn

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
6

Tại đây, bạn đã thiết lập một tệp Python khác đang nhập mô-đun trước đó của bạn,

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
60 và sử dụng
>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8 từ mô-đun đó. Đây là những gì sẽ xảy ra nếu bây giờ bạn chạy
# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
02

# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
0

Trường hợp ngoại lệ được đưa ra trong trường hợp này lại là một

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
62, nhưng lần này thông báo ít hữu ích hơn một chút. Nó cho bạn biết rằng ở đâu đó trong mã, nó dự kiến ​​sẽ hoạt động với một chuỗi, nhưng một số nguyên đã được đưa ra

Di chuyển lên trên, bạn thấy dòng mã đã được thực thi. Sau đó, tệp và số dòng của mã. Tuy nhiên, lần này, thay vì

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
67, chúng ta lấy tên của hàm đang được thực thi,
>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8

Di chuyển đến dòng mã được thực thi tiếp theo, chúng tôi thấy lệnh gọi

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8 có vấn đề của chúng tôi chuyển qua một số nguyên

Đôi khi sau khi một ngoại lệ được đưa ra, một đoạn mã khác sẽ bắt ngoại lệ đó và cũng dẫn đến một ngoại lệ. Trong những tình huống này, Python sẽ xuất ra tất cả các lần theo dõi ngoại lệ theo thứ tự mà chúng được nhận, một lần nữa kết thúc bằng lần theo dõi của ngoại lệ tăng gần đây nhất

Vì điều này có thể hơi khó hiểu, đây là một ví dụ. Thêm cuộc gọi tới

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
75 vào cuối
>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
60

# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
8

Điều này sẽ dẫn đến việc in lời chào cho cả ba người. Tuy nhiên, nếu bạn chạy mã này, bạn sẽ thấy một ví dụ về nhiều truy xuất ngược được xuất ra

# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
9

Lưu ý dòng được đánh dấu bắt đầu bằng

# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
09 ở đầu ra ở trên. Ở giữa tất cả các dấu vết, bạn sẽ thấy dòng này. Thông báo của nó rất rõ ràng, trong khi mã của bạn đang cố xử lý ngoại lệ trước đó, thì một ngoại lệ khác đã được đưa ra

Ghi chú. Tính năng hiển thị truy nguyên ngoại lệ trước đó của Python đã được thêm vào Python 3. Trong Python 2, bạn sẽ chỉ nhận được truy nguyên của ngoại lệ cuối cùng

Bạn đã từng thấy ngoại lệ trước đó, khi bạn gọi

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8 bằng một số nguyên. Vì chúng tôi đã thêm một
# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
81 vào danh sách những người cần chào hỏi, nên chúng tôi có thể mong đợi kết quả tương tự. Tuy nhiên, hàm
>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
75 kết thúc lệnh gọi
>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8 trong khối
# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
84 và
# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
85. Chỉ trong trường hợp
>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8 dẫn đến một ngoại lệ được đưa ra,
>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
75 muốn in một lời chào mặc định

Phần có liên quan của

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
60 được lặp lại ở đây

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
0

Vì vậy, khi

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8 cho kết quả là
>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
62 do đầu vào số nguyên không hợp lệ,
>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
75 sẽ xử lý ngoại lệ đó và cố gắng in một lời chào đơn giản. Ở đây mã kết thúc dẫn đến một ngoại lệ khác, tương tự. Nó vẫn đang cố thêm một chuỗi và một số nguyên

Xem tất cả đầu ra truy nguyên có thể giúp bạn biết đâu có thể là nguyên nhân thực sự của một ngoại lệ. Đôi khi, khi bạn thấy ngoại lệ cuối cùng được đưa ra và kết quả truy nguyên của ngoại lệ đó, bạn vẫn không thể thấy điều gì sai. Trong những trường hợp đó, chuyển sang các trường hợp ngoại lệ trước đó thường giúp bạn hiểu rõ hơn về nguyên nhân gốc rễ

Loại bỏ các quảng cáo

Một số Tracback phổ biến trong Python là gì?

Biết cách đọc truy nguyên Python khi chương trình của bạn phát sinh ngoại lệ có thể rất hữu ích khi bạn lập trình, nhưng biết một số truy nguyên phổ biến hơn cũng có thể tăng tốc quá trình của bạn

Dưới đây là một số trường hợp ngoại lệ phổ biến mà bạn có thể gặp phải, lý do chúng tăng lên và ý nghĩa của chúng cũng như thông tin bạn có thể tìm thấy trong truy nguyên của chúng

# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
92

# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
92 được nâng lên khi bạn cố gắng truy cập một thuộc tính trên một đối tượng không xác định thuộc tính đó. Tài liệu Python xác định khi nào ngoại lệ này được đưa ra

Xảy ra khi tham chiếu hoặc gán thuộc tính không thành công. [Nguồn]

Đây là một ví dụ về

# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
92 được nêu ra

>>>

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
0

Dòng thông báo lỗi cho một

# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
92 cho bạn biết rằng loại đối tượng cụ thể, trong trường hợp này là
# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
96, không có thuộc tính được truy cập, trong trường hợp này là
# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
97. Nhìn thấy
# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
92 trong dòng thông báo lỗi có thể giúp bạn nhanh chóng xác định thuộc tính nào mà bạn đã cố truy cập và nơi để khắc phục sự cố đó

Hầu hết thời gian, việc nhận được ngoại lệ này cho thấy rằng bạn có thể đang làm việc với một đối tượng không phải là loại mà bạn mong đợi

>>>

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
1

Trong ví dụ trên, bạn có thể mong đợi

# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
99 thuộc loại
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
00, có một phương thức gọi là
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
01. Khi bạn nhận được ngoại lệ
# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
92 và thấy rằng nó đã được nâng lên khi bạn đang cố gọi
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
01, điều đó cho bạn biết rằng bạn có thể không xử lý loại đối tượng mà bạn mong đợi

Thông thường, điều này xảy ra khi bạn đang mong đợi một đối tượng được trả về từ một lệnh gọi hàm hoặc phương thức thuộc một loại cụ thể và bạn kết thúc với một đối tượng thuộc loại

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
04. Trong trường hợp này, dòng thông báo lỗi sẽ đọc,
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
05

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
06

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
06 được nâng lên khi xảy ra sự cố với câu lệnh nhập. Bạn sẽ nhận được ngoại lệ này, hoặc lớp con của nó là
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
08, nếu không tìm thấy mô-đun bạn đang cố nhập hoặc nếu bạn cố nhập thứ gì đó từ một mô-đun không tồn tại trong mô-đun. Tài liệu Python xác định khi nào ngoại lệ này được đưa ra

Xảy ra khi câu lệnh nhập gặp sự cố khi cố tải một mô-đun. Cũng được nêu ra khi 'từ danh sách' trong

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
09 có tên không thể tìm thấy. [Nguồn]

Đây là một ví dụ về

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
06 và
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
08 được nâng lên

>>>

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
2

Trong ví dụ trên, bạn có thể thấy rằng việc cố gắng nhập một mô-đun không tồn tại,

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
02, sẽ dẫn đến kết quả là
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
08. Khi cố gắng nhập thứ gì đó không tồn tại,
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
02, từ một mô-đun tồn tại,
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
05, điều này dẫn đến một
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
06. Các dòng thông báo lỗi ở cuối truy vết cho bạn biết thứ nào không thể nhập được,
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
02 trong cả hai trường hợp

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
08

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
08 được nâng lên khi bạn cố gắng truy xuất chỉ mục từ một chuỗi, chẳng hạn như
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
00 hoặc
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
11 và không tìm thấy chỉ mục trong chuỗi. Tài liệu Python xác định khi nào ngoại lệ này được đưa ra

Tăng lên khi một chuỗi con nằm ngoài phạm vi. [Nguồn]

Đây là một ví dụ làm tăng

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
08

>>>

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
3

Dòng thông báo lỗi cho một

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
08 không cung cấp cho bạn thông tin hữu ích. Bạn có thể thấy rằng bạn có tham chiếu trình tự là
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
14 và loại trình tự là gì, trong trường hợp này là
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
00. Thông tin đó, kết hợp với phần còn lại của truy nguyên, thường đủ để giúp bạn nhanh chóng xác định cách khắc phục sự cố

Loại bỏ các quảng cáo

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
16

Tương tự như

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
08,
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
16 được nâng lên khi bạn cố gắng truy cập một khóa không có trong ánh xạ, thường là
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
19. Hãy coi đây là
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
08 nhưng đối với từ điển. Tài liệu Python xác định khi nào ngoại lệ này được đưa ra

Xảy ra khi không tìm thấy khóa ánh xạ [từ điển] trong tập hợp các khóa hiện có. [Nguồn]

Đây là một ví dụ về

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
16 được nâng lên

>>>

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
4

Dòng thông báo lỗi cho

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
16 cung cấp cho bạn khóa không thể tìm thấy. Điều này không có gì nhiều để tiếp tục, nhưng kết hợp với phần còn lại của truy nguyên, thường là đủ để khắc phục sự cố

Để có cái nhìn sâu hơn về

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
16, hãy xem Python KeyError Exceptions và How to Handle Them

# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
3

# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
3 được nâng lên khi bạn đã tham chiếu đến một biến, mô-đun, lớp, hàm hoặc một số tên khác chưa được xác định trong mã của bạn. Tài liệu Python xác định khi nào ngoại lệ này được đưa ra

Xảy ra khi không tìm thấy tên cục bộ hoặc toàn cầu. [Nguồn]

Trong đoạn mã dưới đây,

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8 nhận tham số
# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
8. Nhưng trong chính hàm đó, tham số đó đã bị viết sai chính tả thành
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
28

>>>

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
5

Dòng thông báo lỗi của truy nguyên

# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
3 cung cấp cho bạn tên bị thiếu. Trong ví dụ trên, đó là một biến sai chính tả hoặc tham số của hàm được truyền vào

Một

# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
3 cũng sẽ được nâng lên nếu đó là thông số mà bạn viết sai chính tả

>>>

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
6

Ở đây, có vẻ như bạn không làm gì sai. Dòng cuối cùng đã được thực thi và được tham chiếu trong truy nguyên có vẻ tốt. Nếu bạn thấy mình trong tình huống này, thì điều cần làm là xem qua mã của bạn để biết biến

# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
8 được sử dụng và định nghĩa ở đâu. Tại đây, bạn có thể nhanh chóng thấy rằng tên tham số đã bị viết sai chính tả

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
32

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
32 được nâng lên khi bạn có cú pháp Python không chính xác trong mã của mình. Tài liệu Python xác định khi nào ngoại lệ này được đưa ra

Xảy ra khi trình phân tích cú pháp gặp lỗi cú pháp. [Nguồn]

Dưới đây, vấn đề là thiếu dấu hai chấm ở cuối dòng định nghĩa hàm. Trong Python REPL, lỗi cú pháp này xuất hiện ngay sau khi nhấn enter

>>>

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
7

Dòng thông báo lỗi của

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
32 chỉ cho bạn biết rằng đã xảy ra sự cố với cú pháp mã của bạn. Nhìn vào các dòng trên sẽ cho bạn dòng có vấn đề và thường là một
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
35 [dấu mũ] chỉ vào vị trí có vấn đề. Ở đây, dấu hai chấm bị thiếu trong câu lệnh
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
36 của hàm

Ngoài ra, với truy nguyên

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
32, dòng đầu tiên thông thường
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
38 bị thiếu. Đó là bởi vì
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
32 được nâng lên khi Python cố phân tích cú pháp mã của bạn và các dòng không thực sự được thực thi

Loại bỏ các quảng cáo

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
62

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
62 được nâng lên khi mã của bạn cố gắng làm điều gì đó với một đối tượng không thể làm điều đó, chẳng hạn như cố gắng thêm một chuỗi vào một số nguyên hoặc gọi
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
42 trên một đối tượng không xác định được độ dài của nó. Tài liệu Python xác định khi nào ngoại lệ này được đưa ra

Xảy ra khi một thao tác hoặc chức năng được áp dụng cho một đối tượng thuộc loại không phù hợp. [Nguồn]

Sau đây là một số ví dụ về

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
62 được nêu ra

>>>

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
8

Tất cả các ví dụ trên về việc tăng

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
62 dẫn đến một dòng thông báo lỗi với các thông báo khác nhau. Mỗi người trong số họ làm khá tốt công việc thông báo cho bạn về những gì sai

Hai ví dụ đầu tiên cố gắng cộng các chuỗi và số nguyên với nhau. Tuy nhiên, chúng khác nhau một cách tinh tế

  • Đầu tiên là cố gắng thêm một
    $ python example.py
    Traceback [most recent call last]:
      File "/path/to/example.py", line 4, in 
        greet['Chad']
      File "/path/to/example.py", line 2, in greet
        print['Hello, ' + someon]
    NameError: name 'someon' is not defined
    
    45 vào một
    # greetings.py
    def who_to_greet[person]:
        return person if person else input['Greet who? ']
    
    def greet[someone, greeting='Hello']:
        print[greeting + ', ' + who_to_greet[someone]]
    
    def greet_many[people]:
        for person in people:
            try:
                greet[person]
            except Exception:
                print['hi, ' + person]
    
    96
  • Thứ hai là cố gắng thêm một
    # greetings.py
    def who_to_greet[person]:
        return person if person else input['Greet who? ']
    
    def greet[someone, greeting='Hello']:
        print[greeting + ', ' + who_to_greet[someone]]
    
    def greet_many[people]:
        for person in people:
            try:
                greet[person]
            except Exception:
                print['hi, ' + person]
    
    96 vào một
    $ python example.py
    Traceback [most recent call last]:
      File "/path/to/example.py", line 4, in 
        greet['Chad']
      File "/path/to/example.py", line 2, in greet
        print['Hello, ' + someon]
    NameError: name 'someon' is not defined
    
    45

Các dòng thông báo lỗi phản ánh những khác biệt này

Ví dụ cuối cùng cố gắng gọi

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
42 trên một
# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
96. Dòng thông báo lỗi cho bạn biết rằng bạn không thể làm điều đó với một
# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
96

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
52

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
52 được nâng lên khi giá trị của đối tượng không chính xác. Bạn có thể coi đây là một
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
08 được nâng lên vì giá trị của chỉ mục không nằm trong phạm vi của chuỗi, chỉ có
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
52 dành cho trường hợp chung hơn. Tài liệu Python xác định khi nào ngoại lệ này được đưa ra

Xảy ra khi một hoạt động hoặc chức năng nhận được một đối số có loại phù hợp nhưng giá trị không phù hợp và tình huống không được mô tả bằng một ngoại lệ chính xác hơn, chẳng hạn như

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
08. [Nguồn]

Dưới đây là hai ví dụ về

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
52 được nêu ra

>>>

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
9

Dòng thông báo lỗi

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
52 trong các ví dụ này cho bạn biết chính xác vấn đề xảy ra với các giá trị

  1. Trong ví dụ đầu tiên, bạn đang cố giải nén quá nhiều giá trị. Dòng thông báo lỗi thậm chí còn cho bạn biết rằng bạn định giải nén 3 giá trị nhưng nhận được 2 giá trị

  2. Trong ví dụ thứ hai, vấn đề là bạn nhận được quá nhiều giá trị và không đủ biến để giải nén chúng vào

Làm thế nào để bạn đăng nhập Tracback?

Gặp một ngoại lệ và kết quả truy nguyên Python của nó có nghĩa là bạn cần quyết định phải làm gì với nó. Thông thường, sửa mã của bạn là bước đầu tiên, nhưng đôi khi sự cố xảy ra với đầu vào không mong muốn hoặc không chính xác. Mặc dù việc cung cấp các tình huống đó trong mã của bạn là điều tốt, nhưng đôi khi, việc tắt tiếng hoặc ẩn ngoại lệ bằng cách ghi nhật ký truy nguyên và làm việc khác cũng có ý nghĩa

Đây là một ví dụ thực tế hơn về mã cần tắt một số dấu vết của Python. Ví dụ này sử dụng thư viện

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
59. Bạn có thể tìm hiểu thêm về nó trong Thư viện yêu cầu của Python [Hướng dẫn]

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
0

Mã này hoạt động tốt. Khi bạn chạy tập lệnh này, cung cấp cho nó một URL làm đối số dòng lệnh, nó sẽ gọi URL và sau đó in mã trạng thái HTTP và nội dung từ phản hồi. Nó thậm chí còn hoạt động nếu phản hồi là trạng thái lỗi HTTP

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
1

Tuy nhiên, đôi khi URL mà tập lệnh của bạn được cung cấp để truy xuất không tồn tại hoặc máy chủ lưu trữ không hoạt động. Trong những trường hợp đó, tập lệnh này giờ đây sẽ đưa ra một ngoại lệ

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
60 chưa được phát hiện và in một dấu vết

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
2

Truy nguyên Python ở đây có thể rất dài với nhiều trường hợp ngoại lệ khác được nêu ra và cuối cùng dẫn đến việc

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
60 được tăng lên bởi chính
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
59. Nếu bạn di chuyển lên truy nguyên ngoại lệ cuối cùng, bạn có thể thấy rằng tất cả vấn đề bắt đầu trong mã của chúng tôi với dòng 5 của
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
63

Nếu bạn bọc dòng vi phạm trong khối

# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
84 và
# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
85, việc nắm bắt ngoại lệ thích hợp sẽ cho phép tập lệnh của bạn tiếp tục hoạt động với nhiều đầu vào hơn

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
3

Đoạn mã trên sử dụng mệnh đề

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
66 với khối
# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
84 và
# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
85. Nếu bạn không quen với tính năng này của Python, hãy xem phần về mệnh đề
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
66 trong Ngoại lệ Python. Một lời giới thiệu

Bây giờ khi bạn chạy tập lệnh với một URL sẽ dẫn đến một

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
60 được nâng lên, bạn sẽ được in một
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
71 cho mã trạng thái và nội dung
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
72

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
4

Điều này làm việc tuyệt vời. Tuy nhiên, trong hầu hết các hệ thống thực tế, bạn không muốn chỉ tắt ngoại lệ và dẫn đến truy nguyên, mà bạn muốn ghi nhật ký truy nguyên. Ghi nhật ký truy nguyên cho phép bạn hiểu rõ hơn về những gì sai trong chương trình của mình

Ghi chú. Để tìm hiểu thêm về hệ thống ghi nhật ký của Python, hãy xem Đăng nhập bằng Python

Bạn có thể đăng nhập truy nguyên trong tập lệnh bằng cách nhập gói

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
73, lấy bộ ghi và gọi
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
74 trên bộ ghi đó trong phần
# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
85 của khối
# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
84 và
# greetings.py
def who_to_greet[person]:
    return person if person else input['Greet who? ']

def greet[someone, greeting='Hello']:
    print[greeting + ', ' + who_to_greet[someone]]

def greet_many[people]:
    for person in people:
        try:
            greet[person]
        except Exception:
            print['hi, ' + person]
85. Tập lệnh cuối cùng của bạn sẽ trông giống như đoạn mã sau

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
5

Bây giờ, khi bạn chạy tập lệnh cho một URL có vấn đề, nó sẽ in

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
71 và
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
72 dự kiến, nhưng nó cũng sẽ ghi lại quá trình truy nguyên

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
6

Theo mặc định, Python sẽ gửi thông báo tường trình tới lỗi tiêu chuẩn [

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
80]. Điều này có vẻ như chúng tôi đã không loại bỏ đầu ra truy nguyên. Tuy nhiên, nếu bạn gọi lại nó trong khi chuyển hướng
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
80, bạn có thể thấy rằng hệ thống ghi nhật ký đang hoạt động và chúng tôi có thể lưu các nhật ký của mình để sử dụng sau

>>> def greet[someone]:
..   print['Hello, ' + someon]
.. 
>>> greet['Chad']
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
7

Loại bỏ các quảng cáo

Phần kết luận

Traceback Python chứa thông tin tuyệt vời có thể giúp bạn tìm thấy những gì đang xảy ra trong mã Python của bạn. Những dấu vết này có thể trông hơi đáng sợ, nhưng một khi bạn chia nhỏ nó ra để xem những gì nó đang cố cho bạn thấy, chúng có thể cực kỳ hữu ích. Đi qua từng dòng một vài dấu vết sẽ giúp bạn hiểu rõ hơn về thông tin chúng chứa và giúp bạn tận dụng tối đa chúng

Nhận đầu ra truy nguyên Python khi bạn chạy mã là cơ hội để cải thiện mã của bạn. Đó là một cách Python cố gắng giúp bạn

Bây giờ bạn đã biết cách đọc truy nguyên Python, bạn có thể hưởng lợi từ việc tìm hiểu thêm về một số công cụ và kỹ thuật để chẩn đoán các vấn đề mà đầu ra truy ngược của bạn đang cho bạn biết. Có thể sử dụng mô-đun

$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
82 tích hợp sẵn của Python để làm việc và kiểm tra truy nguyên. Mô-đun
$ python example.py
Traceback [most recent call last]:
  File "/path/to/example.py", line 4, in 
    greet['Chad']
  File "/path/to/example.py", line 2, in greet
    print['Hello, ' + someon]
NameError: name 'someon' is not defined
82 có thể hữu ích khi bạn cần khai thác nhiều hơn từ đầu ra truy nguyên. Sẽ rất hữu ích nếu bạn tìm hiểu thêm về một số kỹ thuật gỡ lỗi mã Python của bạn và các cách gỡ lỗi trong IDLE

Đánh dấu là đã hoàn thành

Xem ngay Hướng dẫn này có một khóa học video liên quan do nhóm Real Python tạo. Xem nó cùng với hướng dẫn bằng văn bản để hiểu sâu hơn. Khai thác tối đa khả năng truy nguyên của Python

🐍 Thủ thuật Python 💌

Nhận một Thủ thuật Python ngắn và hấp dẫn được gửi đến hộp thư đến của bạn vài ngày một lần. Không có thư rác bao giờ. Hủy đăng ký bất cứ lúc nào. Được quản lý bởi nhóm Real Python

Gửi cho tôi thủ thuật Python »

Giới thiệu về Chad Hansen

Chad là một Pythonista cuồng nhiệt và phát triển web toàn thời gian với Django. Chad sống ở Utah với vợ và sáu đứa con

» Tìm hiểu thêm về Tchad

Mỗi hướng dẫn tại Real Python được tạo bởi một nhóm các nhà phát triển để nó đáp ứng các tiêu chuẩn chất lượng cao của chúng tôi. Các thành viên trong nhóm đã làm việc trong hướng dẫn này là

Aldren

Joanna

Mike

Bậc thầy Kỹ năng Python trong thế giới thực Với quyền truy cập không giới hạn vào Python thực

Tham gia với chúng tôi và có quyền truy cập vào hàng nghìn hướng dẫn, khóa học video thực hành và cộng đồng các Pythonistas chuyên gia

Nâng cao kỹ năng Python của bạn »

Bậc thầy Kỹ năng Python trong thế giới thực
Với quyền truy cập không giới hạn vào Python thực

Tham gia với chúng tôi và có quyền truy cập vào hàng ngàn hướng dẫn, khóa học video thực hành và cộng đồng Pythonistas chuyên gia

Nâng cao kỹ năng Python của bạn »

Bạn nghĩ sao?

Đánh giá bài viết này

Tweet Chia sẻ Chia sẻ Email

Bài học số 1 hoặc điều yêu thích mà bạn đã học được là gì?

Mẹo bình luận. Những nhận xét hữu ích nhất là những nhận xét được viết với mục đích học hỏi hoặc giúp đỡ các sinh viên khác. Nhận các mẹo để đặt câu hỏi hay và nhận câu trả lời cho các câu hỏi phổ biến trong cổng thông tin hỗ trợ của chúng tôi

OSError trong Python là gì?

hệ điều hành. lỗi trong Python là lớp lỗi cho tất cả các lỗi I/O và là bí danh của ngoại lệ OSError. Tất cả các phương thức có trong mô-đun HĐH sẽ nâng hệ điều hành. ngoại lệ lỗi khi đường dẫn tệp không thể truy cập hoặc không hợp lệ được chỉ định

Python xử lý lỗi thời gian chạy như thế nào?

Không thể xử lý lỗi , trong khi các ngoại lệ Python có thể được xử lý trong thời gian chạy. Lỗi có thể là lỗi cú pháp [phân tích cú pháp], trong khi có thể có nhiều loại ngoại lệ có thể xảy ra trong quá trình thực thi và không phải là không thể hoạt động vô điều kiện.

Python xử lý lỗi đầu vào như thế nào?

Các trường hợp ngoại lệ xảy ra do đầu vào không hợp lệ có thể được quản lý bằng cách sử dụng khối thử ngoại trừ . Chức năng split và map giúp nhập nhiều giá trị trên cùng một dòng.

Chủ Đề