Các lỗi truy nguyên phổ biến trong Python là gì?

Mô-đun này cung cấp một giao diện tiêu chuẩn để trích xuất, định dạng và in dấu vết ngăn xếp của các chương trình Python. Nó bắt chước chính xác hành vi của trình thông dịch Python khi nó in dấu vết ngăn xếp. Điều này hữu ích khi bạn muốn in dấu vết ngăn xếp dưới sự kiểm soát của chương trình, chẳng hạn như trong một “trình bao bọc” xung quanh trình thông dịch

Mô-đun sử dụng các đối tượng theo dõi — đây là loại đối tượng được lưu trữ trong biến và được trả về dưới dạng mục thứ ba từ

Mô-đun xác định các chức năng sau

truy xuất. print_tb[tb , giới hạn=Không có , tệp=Không có]

In lên để giới hạn các mục theo dõi ngăn xếp từ đối tượng truy nguyên tb [bắt đầu từ khung của người gọi] nếu giới hạn là dương. Nếu không, hãy in các mục

import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
7 cuối cùng. Nếu giới hạn bị bỏ qua hoặc
import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
8, tất cả các mục được in. Nếu tệp bị bỏ qua hoặc
import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
8, đầu ra sẽ là
import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
1;

Đã thay đổi trong phiên bản 3. 5. Đã thêm hỗ trợ giới hạn âm.

truy xuất. print_Exception[exc , / , [giá trị , tb , ]limit=None , file=None , chain=True]

In thông tin ngoại lệ và ngăn xếp các mục theo dõi từ đối tượng truy nguyên tb vào tệp. Điều này khác với những cách sau

  • nếu tb không phải là

    import sys, traceback
    
    def lumberjack[]:
        bright_side_of_life[]
    
    def bright_side_of_life[]:
        return tuple[][0]
    
    try:
        lumberjack[]
    except IndexError:
        exc_type, exc_value, exc_traceback = sys.exc_info[]
        print["*** print_tb:"]
        traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
        print["*** print_exception:"]
        traceback.print_exception[exc_value, limit=2, file=sys.stdout]
        print["*** print_exc:"]
        traceback.print_exc[limit=2, file=sys.stdout]
        print["*** format_exc, first and last line:"]
        formatted_lines = traceback.format_exc[].splitlines[]
        print[formatted_lines[0]]
        print[formatted_lines[-1]]
        print["*** format_exception:"]
        print[repr[traceback.format_exception[exc_value]]]
        print["*** extract_tb:"]
        print[repr[traceback.extract_tb[exc_traceback]]]
        print["*** format_tb:"]
        print[repr[traceback.format_tb[exc_traceback]]]
        print["*** tb_lineno:", exc_traceback.tb_lineno]
    
    8, nó sẽ in tiêu đề
    import sys, traceback
    
    def lumberjack[]:
        bright_side_of_life[]
    
    def bright_side_of_life[]:
        return tuple[][0]
    
    try:
        lumberjack[]
    except IndexError:
        exc_type, exc_value, exc_traceback = sys.exc_info[]
        print["*** print_tb:"]
        traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
        print["*** print_exception:"]
        traceback.print_exception[exc_value, limit=2, file=sys.stdout]
        print["*** print_exc:"]
        traceback.print_exc[limit=2, file=sys.stdout]
        print["*** format_exc, first and last line:"]
        formatted_lines = traceback.format_exc[].splitlines[]
        print[formatted_lines[0]]
        print[formatted_lines[-1]]
        print["*** format_exception:"]
        print[repr[traceback.format_exception[exc_value]]]
        print["*** extract_tb:"]
        print[repr[traceback.extract_tb[exc_traceback]]]
        print["*** format_tb:"]
        print[repr[traceback.format_tb[exc_traceback]]]
        print["*** tb_lineno:", exc_traceback.tb_lineno]
    
    4

  • nó in loại và giá trị ngoại lệ sau dấu vết ngăn xếp

  • nếu loại [giá trị] là và giá trị có định dạng phù hợp, nó sẽ in dòng xảy ra lỗi cú pháp với dấu mũ cho biết vị trí gần đúng của lỗi

Kể từ Python 3. 10, thay vì truyền giá trị và tb, một đối tượng ngoại lệ có thể được truyền làm đối số đầu tiên. Nếu giá trị và tb được cung cấp, đối số đầu tiên sẽ bị bỏ qua để cung cấp khả năng tương thích ngược

Đối số giới hạn tùy chọn có cùng ý nghĩa như đối với. Nếu chuỗi là đúng [mặc định], thì các ngoại lệ chuỗi [thuộc tính

import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
7 hoặc
import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
8 của ngoại lệ] cũng sẽ được in, giống như chính trình thông dịch thực hiện khi in một ngoại lệ chưa được xử lý

Đã thay đổi trong phiên bản 3. 5. Đối số etype bị bỏ qua và được suy ra từ loại giá trị.

Đã thay đổi trong phiên bản 3. 10. Tham số etype đã được đổi tên thành ex và hiện chỉ có vị trí.

truy xuất. print_exc[giới hạn=Không có , tệp=Không , chuỗi=Đúng]

Đây là cách viết tắt của

import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
9

truy xuất. print_last[giới hạn=Không có , tệp=Không , chuỗi=Đúng]

Đây là cách viết tắt của

*** print_tb:
  File "", line 10, in 
    lumberjack[]
*** print_exception:
Traceback [most recent call last]:
  File "", line 10, in 
    lumberjack[]
  File "", line 4, in lumberjack
    bright_side_of_life[]
IndexError: tuple index out of range
*** print_exc:
Traceback [most recent call last]:
  File "", line 10, in 
    lumberjack[]
  File "", line 4, in lumberjack
    bright_side_of_life[]
IndexError: tuple index out of range
*** format_exc, first and last line:
Traceback [most recent call last]:
IndexError: tuple index out of range
*** format_exception:
['Traceback [most recent call last]:\n',
 '  File "", line 10, in \n    lumberjack[]\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life[]\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple[][0]\n           ~~~~~~~^^^\n',
 'IndexError: tuple index out of range\n']
*** extract_tb:
[,
 ,
 ]
*** format_tb:
['  File "", line 10, in \n    lumberjack[]\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life[]\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple[][0]\n           ~~~~~~~^^^\n']
*** tb_lineno: 10
0. Nói chung, nó sẽ chỉ hoạt động sau khi một ngoại lệ đạt đến dấu nhắc tương tác [xem phần ]

truy xuất. print_stack[f=Không , giới hạn=Không có , tệp=Không có]

In lên để giới hạn các mục theo dõi ngăn xếp [bắt đầu từ điểm gọi] nếu giới hạn là dương. Nếu không, hãy in các mục

import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
7 cuối cùng. Nếu giới hạn bị bỏ qua hoặc
import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
8, tất cả các mục được in. Đối số f tùy chọn có thể được sử dụng để chỉ định khung ngăn xếp thay thế bắt đầu. Đối số tệp tùy chọn có cùng ý nghĩa như đối với

Đã thay đổi trong phiên bản 3. 5. Đã thêm hỗ trợ giới hạn âm.

truy xuất. extract_tb[tb , giới hạn=Không có]

Trả về một đối tượng đại diện cho một danh sách các mục theo dõi ngăn xếp “được xử lý trước” được trích xuất từ ​​đối tượng truy nguyên tb. Nó rất hữu ích cho việc định dạng thay thế các dấu vết ngăn xếp. Đối số giới hạn tùy chọn có cùng ý nghĩa như đối với. Một mục theo dõi ngăn xếp “được xử lý trước” là một đối tượng chứa các thuộc tính

*** print_tb:
  File "", line 10, in 
    lumberjack[]
*** print_exception:
Traceback [most recent call last]:
  File "", line 10, in 
    lumberjack[]
  File "", line 4, in lumberjack
    bright_side_of_life[]
IndexError: tuple index out of range
*** print_exc:
Traceback [most recent call last]:
  File "", line 10, in 
    lumberjack[]
  File "", line 4, in lumberjack
    bright_side_of_life[]
IndexError: tuple index out of range
*** format_exc, first and last line:
Traceback [most recent call last]:
IndexError: tuple index out of range
*** format_exception:
['Traceback [most recent call last]:\n',
 '  File "", line 10, in \n    lumberjack[]\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life[]\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple[][0]\n           ~~~~~~~^^^\n',
 'IndexError: tuple index out of range\n']
*** extract_tb:
[,
 ,
 ]
*** format_tb:
['  File "", line 10, in \n    lumberjack[]\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life[]\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple[][0]\n           ~~~~~~~^^^\n']
*** tb_lineno: 10
8,
*** print_tb:
  File "", line 10, in 
    lumberjack[]
*** print_exception:
Traceback [most recent call last]:
  File "", line 10, in 
    lumberjack[]
  File "", line 4, in lumberjack
    bright_side_of_life[]
IndexError: tuple index out of range
*** print_exc:
Traceback [most recent call last]:
  File "", line 10, in 
    lumberjack[]
  File "", line 4, in lumberjack
    bright_side_of_life[]
IndexError: tuple index out of range
*** format_exc, first and last line:
Traceback [most recent call last]:
IndexError: tuple index out of range
*** format_exception:
['Traceback [most recent call last]:\n',
 '  File "", line 10, in \n    lumberjack[]\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life[]\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple[][0]\n           ~~~~~~~^^^\n',
 'IndexError: tuple index out of range\n']
*** extract_tb:
[,
 ,
 ]
*** format_tb:
['  File "", line 10, in \n    lumberjack[]\n',
 '  File "", line 4, in lumberjack\n    bright_side_of_life[]\n',
 '  File "", line 7, in bright_side_of_life\n    return tuple[][0]\n           ~~~~~~~^^^\n']
*** tb_lineno: 10
9,
>>> import traceback
>>> def another_function[]:
..     lumberstack[]
...
>>> def lumberstack[]:
..     traceback.print_stack[]
..     print[repr[traceback.extract_stack[]]]
..     print[repr[traceback.format_stack[]]]
...
>>> another_function[]
  File "", line 10, in 
    another_function[]
  File "", line 3, in another_function
    lumberstack[]
  File "", line 6, in lumberstack
    traceback.print_stack[]
[['', 10, '', 'another_function[]'],
 ['', 3, 'another_function', 'lumberstack[]'],
 ['', 7, 'lumberstack', 'print[repr[traceback.extract_stack[]]]']]
['  File "", line 10, in \n    another_function[]\n',
 '  File "", line 3, in another_function\n    lumberstack[]\n',
 '  File "", line 8, in lumberstack\n    print[repr[traceback.format_stack[]]]\n']
0 và
>>> import traceback
>>> def another_function[]:
..     lumberstack[]
...
>>> def lumberstack[]:
..     traceback.print_stack[]
..     print[repr[traceback.extract_stack[]]]
..     print[repr[traceback.format_stack[]]]
...
>>> another_function[]
  File "", line 10, in 
    another_function[]
  File "", line 3, in another_function
    lumberstack[]
  File "", line 6, in lumberstack
    traceback.print_stack[]
[['', 10, '', 'another_function[]'],
 ['', 3, 'another_function', 'lumberstack[]'],
 ['', 7, 'lumberstack', 'print[repr[traceback.extract_stack[]]]']]
['  File "", line 10, in \n    another_function[]\n',
 '  File "", line 3, in another_function\n    lumberstack[]\n',
 '  File "", line 8, in lumberstack\n    print[repr[traceback.format_stack[]]]\n']
1 biểu thị thông tin thường được in cho một lần theo dõi ngăn xếp.
>>> import traceback
>>> def another_function[]:
..     lumberstack[]
...
>>> def lumberstack[]:
..     traceback.print_stack[]
..     print[repr[traceback.extract_stack[]]]
..     print[repr[traceback.format_stack[]]]
...
>>> another_function[]
  File "", line 10, in 
    another_function[]
  File "", line 3, in another_function
    lumberstack[]
  File "", line 6, in lumberstack
    traceback.print_stack[]
[['', 10, '', 'another_function[]'],
 ['', 3, 'another_function', 'lumberstack[]'],
 ['', 7, 'lumberstack', 'print[repr[traceback.extract_stack[]]]']]
['  File "", line 10, in \n    another_function[]\n',
 '  File "", line 3, in another_function\n    lumberstack[]\n',
 '  File "", line 8, in lumberstack\n    print[repr[traceback.format_stack[]]]\n']
1 là một chuỗi có khoảng trắng ở đầu và cuối bị bỏ đi;

truy xuất. extract_stack[f=Không , giới hạn=Không có]

Trích xuất truy nguyên thô từ khung ngăn xếp hiện tại. Giá trị trả về có cùng định dạng như đối với. Các đối số f và giới hạn tùy chọn có cùng ý nghĩa như đối với

truy xuất. format_list[extracted_list]

Đưa ra một danh sách các bộ hoặc đối tượng được trả về bởi hoặc , trả về một danh sách các chuỗi sẵn sàng để in. Mỗi chuỗi trong danh sách kết quả tương ứng với mục có cùng chỉ mục trong danh sách đối số. Mỗi chuỗi kết thúc bằng một dòng mới;

truy xuất. format_exception_only[exc , /[ , giá trị]]

Định dạng phần ngoại lệ của truy nguyên bằng cách sử dụng giá trị ngoại lệ, chẳng hạn như được cung cấp bởi

import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
10. Giá trị trả về là một danh sách các chuỗi, mỗi chuỗi kết thúc bằng một dòng mới. Thông thường, danh sách chứa một chuỗi đơn; . Thông báo cho biết ngoại lệ nào xảy ra luôn là chuỗi cuối cùng trong danh sách

Kể từ Python 3. 10, thay vì truyền giá trị, một đối tượng ngoại lệ có thể được truyền làm đối số đầu tiên. Nếu giá trị được cung cấp, đối số đầu tiên sẽ bị bỏ qua để cung cấp khả năng tương thích ngược

Đã thay đổi trong phiên bản 3. 10. Tham số etype đã được đổi tên thành ex và hiện chỉ có vị trí.

truy xuất. format_Exception[exc , / , [giá trị , tb , ]limit=None , chain=True]

Định dạng dấu vết ngăn xếp và thông tin ngoại lệ. Các đối số có cùng ý nghĩa với các đối số tương ứng với. Giá trị trả về là một danh sách các chuỗi, mỗi chuỗi kết thúc bằng một dòng mới và một số chứa các dòng mới bên trong. Khi các dòng này được nối với nhau và được in ra, chính xác văn bản được in giống như

Đã thay đổi trong phiên bản 3. 5. Đối số etype bị bỏ qua và được suy ra từ loại giá trị.

Đã thay đổi trong phiên bản 3. 10. Hành vi và chữ ký của chức năng này đã được sửa đổi để phù hợp.

truy xuất. format_exc[giới hạn=Không , chuỗi=Đúng]

Điều này giống như

import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
15 nhưng trả về một chuỗi thay vì in ra một tệp

truy xuất. format_tb[tb , giới hạn=Không có]

Viết tắt của

import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
16

truy xuất. format_stack[f=Không , giới hạn=Không có]

Viết tắt của

import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
17

truy xuất. clear_frames[tb]

Xóa các biến cục bộ của tất cả các khung ngăn xếp trong tb truy nguyên bằng cách gọi phương thức

import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
18 của từng đối tượng khung

Mới trong phiên bản 3. 4

truy xuất. walk_stack[f]

Di chuyển một ngăn xếp theo sau

import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
19 từ khung đã cho, mang lại khung và số dòng cho mỗi khung. Nếu f là
import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
8, ngăn xếp hiện tại được sử dụng. Người trợ giúp này được sử dụng với

Mới trong phiên bản 3. 5

truy xuất. walk_tb[tb]

Đi theo một truy nguyên sau

import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
62 mang lại số khung và số dòng cho mỗi khung. Người trợ giúp này được sử dụng với

Mới trong phiên bản 3. 5

Mô-đun này cũng định nghĩa các lớp sau

Các đối tượng

Mới trong phiên bản 3. 5

các đối tượng được tạo từ các ngoại lệ thực tế để thu thập dữ liệu để in sau này một cách nhẹ nhàng

lớp truy nguyên. TracebackException[exc_type , exc_value, exc_traceback, *, limit=None, lookup_lines=True, capture_locals=False, compact=False]

Nắm bắt một ngoại lệ để hiển thị sau này. giới hạn, lookup_lines và capture_locals dành cho lớp

Nếu compact là đúng, chỉ dữ liệu được yêu cầu bởi phương pháp của ’s

import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
68 được lưu trong thuộc tính lớp. Cụ thể, trường
import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
8 chỉ được tính nếu
import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
7 là
import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
8 và
import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
92 là sai

Lưu ý rằng khi người dân địa phương được chụp, chúng cũng được hiển thị trong truy nguyên

__nguyên nhân__

A của bản gốc

import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
7

__bối cảnh__

A của bản gốc

import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
8

__suppress_context__

Giá trị

import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
92 từ ngoại lệ ban đầu

__ghi chú__

Giá trị

import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
98 từ ngoại lệ ban đầu hoặc
import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
8 nếu ngoại lệ không có bất kỳ ghi chú nào. Nếu nó không phải là
import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
8 thì nó có được định dạng trong truy nguyên sau chuỗi ngoại lệ không

Mới trong phiên bản 3. 11

cây rơm

A đại diện cho truy nguyên

exc_type

Lớp của truy nguyên ban đầu

tên tập tin

Đối với lỗi cú pháp - tên tệp xảy ra lỗi

vải lanh

Đối với lỗi cú pháp - số dòng xảy ra lỗi

chữ

Đối với lỗi cú pháp - văn bản xảy ra lỗi

bù lại

Đối với lỗi cú pháp - phần bù vào văn bản xảy ra lỗi

tin nhắn

Đối với lỗi cú pháp - thông báo lỗi trình biên dịch

classmethod from_Exception[exc , *, limit=None, lookup_lines=True, capture_locals=False]

Nắm bắt một ngoại lệ để hiển thị sau này. giới hạn, lookup_lines và capture_locals dành cho lớp

Lưu ý rằng khi người dân địa phương được chụp, chúng cũng được hiển thị trong truy nguyên

in[* , tệp=None, chain=True]

In ra tệp [mặc định

import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
1] thông tin ngoại lệ được trả về bởi

Mới trong phiên bản 3. 11

định dạng[* , chuỗi=True]

Định dạng ngoại lệ

Nếu chuỗi không phải là

import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
45, thì
import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
7 và
import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
8 sẽ không được định dạng

Giá trị trả về là một trình tạo các chuỗi, mỗi chuỗi kết thúc bằng một dòng mới và một số chứa các dòng mới bên trong. là một trình bao bọc xung quanh phương thức này, nó chỉ in các dòng vào một tệp

Thông báo cho biết ngoại lệ nào xảy ra luôn là chuỗi cuối cùng trong đầu ra

format_Exception_only[]

Định dạng phần ngoại lệ của truy nguyên

Giá trị trả về là một trình tạo chuỗi, mỗi chuỗi kết thúc bằng một dòng mới

Thông thường, trình tạo phát ra một chuỗi duy nhất;

Thông báo cho biết ngoại lệ nào xảy ra luôn là chuỗi cuối cùng trong đầu ra

Đã thay đổi trong phiên bản 3. 10. Đã thêm tham số nhỏ gọn.

Các đối tượng

Mới trong phiên bản 3. 5

các đối tượng đại diện cho một ngăn xếp cuộc gọi đã sẵn sàng để định dạng

lớp truy nguyên. StackSummaryphương thức lớp trích xuất[frame_gen, *, limit=None, lookup_lines=True, capture_locals=False]

Tạo đối tượng từ trình tạo khung [chẳng hạn như được trả về bởi hoặc ]

Nếu giới hạn được cung cấp, chỉ có nhiều khung này được lấy từ frame_gen. Nếu lookup_lines là

import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
75, các đối tượng được trả về sẽ chưa đọc các dòng của chúng, khiến chi phí tạo rẻ hơn [có thể có giá trị nếu nó không thực sự được định dạng]. Nếu capture_locals là
import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
45 thì các biến cục bộ trong mỗi biến được ghi lại dưới dạng biểu diễn đối tượng

classmethod from_list[a_list]

Xây dựng một đối tượng từ danh sách đối tượng được cung cấp hoặc danh sách bộ dữ liệu kiểu cũ. Mỗi bộ phải là 4 bộ với tên tệp, lineno, tên, dòng là các phần tử

định dạng[]

Trả về danh sách các chuỗi đã sẵn sàng để in. Mỗi chuỗi trong danh sách kết quả tương ứng với một khung từ ngăn xếp. Mỗi chuỗi kết thúc bằng một dòng mới;

Đối với các chuỗi dài của cùng một khung và dòng, một vài lần lặp lại đầu tiên được hiển thị, theo sau là một dòng tóm tắt cho biết chính xác số lần lặp lại tiếp theo

Đã thay đổi trong phiên bản 3. 6. Chuỗi khung dài lặp đi lặp lại hiện được viết tắt.

format_frame_summary[frame_summary]

Trả về một chuỗi để in một trong các khung liên quan đến ngăn xếp. Phương thức này được gọi cho từng đối tượng được in bởi. Nếu nó trả về

import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
8, khung sẽ bị loại bỏ khỏi đầu ra

Mới trong phiên bản 3. 11

Các đối tượng

Mới trong phiên bản 3. 5

Một đối tượng đại diện cho một khung hình duy nhất trong truy nguyên

lớp truy nguyên. Tóm tắt khung[tên tệp , lineno, name, lookup_line=True, locals=None, line=None]

Đại diện cho một khung duy nhất trong truy nguyên hoặc ngăn xếp đang được định dạng hoặc in. Nó có thể tùy chọn có một phiên bản chuỗi của các khung cục bộ được bao gồm trong đó. Nếu lookup_line là

import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
75, thì mã nguồn sẽ không được tra cứu cho đến khi thuộc tính
>>> import traceback
>>> def another_function[]:
..     lumberstack[]
...
>>> def lumberstack[]:
..     traceback.print_stack[]
..     print[repr[traceback.extract_stack[]]]
..     print[repr[traceback.format_stack[]]]
...
>>> another_function[]
  File "", line 10, in 
    another_function[]
  File "", line 3, in another_function
    lumberstack[]
  File "", line 6, in lumberstack
    traceback.print_stack[]
[['', 10, '', 'another_function[]'],
 ['', 3, 'another_function', 'lumberstack[]'],
 ['', 7, 'lumberstack', 'print[repr[traceback.extract_stack[]]]']]
['  File "", line 10, in \n    another_function[]\n',
 '  File "", line 3, in another_function\n    lumberstack[]\n',
 '  File "", line 8, in lumberstack\n    print[repr[traceback.format_stack[]]]\n']
1 được truy cập [điều này cũng xảy ra khi truyền nó tới một bộ dữ liệu].
>>> import traceback
>>> def another_function[]:
..     lumberstack[]
...
>>> def lumberstack[]:
..     traceback.print_stack[]
..     print[repr[traceback.extract_stack[]]]
..     print[repr[traceback.format_stack[]]]
...
>>> another_function[]
  File "", line 10, in 
    another_function[]
  File "", line 3, in another_function
    lumberstack[]
  File "", line 6, in lumberstack
    traceback.print_stack[]
[['', 10, '', 'another_function[]'],
 ['', 3, 'another_function', 'lumberstack[]'],
 ['', 7, 'lumberstack', 'print[repr[traceback.extract_stack[]]]']]
['  File "", line 10, in \n    another_function[]\n',
 '  File "", line 3, in another_function\n    lumberstack[]\n',
 '  File "", line 8, in lumberstack\n    print[repr[traceback.format_stack[]]]\n']
1 có thể được cung cấp trực tiếp và sẽ ngăn chặn việc tra cứu dòng xảy ra. local là một từ điển biến cục bộ tùy chọn và nếu được cung cấp, các biểu diễn biến được lưu trữ trong bản tóm tắt để hiển thị sau này

Ví dụ truy nguyên

Ví dụ đơn giản này triển khai vòng lặp đọc-đánh giá-in cơ bản, tương tự [nhưng ít hữu ích hơn] vòng lặp trình thông dịch tương tác Python tiêu chuẩn. Để triển khai vòng lặp thông dịch đầy đủ hơn, hãy tham khảo mô-đun

import sys, traceback

def lumberjack[]:
    bright_side_of_life[]

def bright_side_of_life[]:
    return tuple[][0]

try:
    lumberjack[]
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info[]
    print["*** print_tb:"]
    traceback.print_tb[exc_traceback, limit=1, file=sys.stdout]
    print["*** print_exception:"]
    traceback.print_exception[exc_value, limit=2, file=sys.stdout]
    print["*** print_exc:"]
    traceback.print_exc[limit=2, file=sys.stdout]
    print["*** format_exc, first and last line:"]
    formatted_lines = traceback.format_exc[].splitlines[]
    print[formatted_lines[0]]
    print[formatted_lines[-1]]
    print["*** format_exception:"]
    print[repr[traceback.format_exception[exc_value]]]
    print["*** extract_tb:"]
    print[repr[traceback.extract_tb[exc_traceback]]]
    print["*** format_tb:"]
    print[repr[traceback.format_tb[exc_traceback]]]
    print["*** tb_lineno:", exc_traceback.tb_lineno]
6

Ví dụ sau minh họa các cách khác nhau để in và định dạng ngoại lệ và truy nguyên

3 loại lỗi trong Python là gì?

Có ba loại lỗi chính có thể phân biệt được trong Python. lỗi cú pháp, ngoại lệ và lỗi logic .

Tại sao tôi gặp lỗi truy nguyên trong Python?

Dòng thông báo lỗi của truy nguyên NameError 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 .

Ba yếu tố mà bạn có thể tìm thấy trong truy nguyên là gì?

Lớp của truy nguyên ban đầu. Đối với lỗi cú pháp - tên tệp xảy ra lỗi. Đối với lỗi cú pháp - số dòng xảy ra lỗi. Đối với lỗi cú pháp - văn bản xảy ra lỗi

Các lỗi có thể xảy ra trong chương trình Python là gì?

Trong python có ba loại lỗi; . syntax errors, logic errors and exceptions.

Chủ Đề