Learn Python Traceback

Tram Ho

Python prints a traceback when an exception is raised when running your code. The printed traceback can be overwhelming if first seen or you can’t understand what it says but find the error to fix. But Python traceback has a lot of information that can help you speculate and fix the reason for the exception in your code. Understanding the information of a Python traceback will make it easier for you to code

1. So what is Python traceback?

A traceback is a report containing functions implemented in your code at a specific time. Traceback is known for many names, including stack trace, strack traceback, backtrace, … When the result of your program is an exception, Python will print the traceback immediately to help you know where you are wrong. Here is an example:

The printed traceback has all the information you will need to resolve this error. The last line of the traceback will tell you what kind of exception was raised with some information about the error. The previous line indicates the code that led to the error. On the above traceback, the exception named NameError, with meaning related to the name (variable, function, class) is not defined. In this case, the name is someon

2. How to read a python traceback?

Python traceback includes lots of useful information when you try to determine the reason for an exception in the code. In this section you will go through different tracebacks to understand their different information.

Python Traceback Overview

There are several parts to every Python access that are very important. The diagram below highlights the different parts:

In Python, the best way to read traceback is from bottom to top

  • blue: the last line of the traceback is the error message, it includes the exception name
  • green: after exception is an error message, the message usually contains helpful information that helps understand why
  • yellow: divided into lines of related functions. Each line includes information such as file name, line number, module name, specifying the code you can find
  • red: the second line of each line above, only exactly how the code executes

3. Some common tracebacks in python

Know how to read a traceback when an exception is raised and be especially aware of common tracebacks that will help you speed up the exception handling process.Here are some common exceptions you may encounter, the cause of out, what does it mean

AttributeError

The AttributionError is triggered when you try to access an attribute on an object that does not have that property defined. The Python document identifies when this exception is raised:

The error message line for AttributionError tells you that the specific object type, int in this case, has no property accessed, an_attribution in this case. Viewing the AttributionError in the error message line can help you quickly identify which property you were trying to access and where to fix it.

Most of the time, getting this exception shows that you may be working with an object that is not the type you were expecting:

In the above example, you can expect a_list of type list, there is a method called .append (). When you receive the exception AttributionError and see that it was raised when you were trying to call .append (), that tells you that you may have handled the type of object you were expecting. Typically, this happens when you are expecting an object to be returned from a call or method of a specific type, and you end up with an object of type None. In this case, the error message line will read, AttributionError: object ‘noneType’ does not have the attribute ‘append’.

ImportError

ImportError is raised when there is a problem with the import statement. You can get its exception ModuleNotFoundError or subclass, if the module you are trying to import can be found or if you try to import something from a module that doesn’t exist.

In the above example, we can see that trying to import a module does not exist, resulting in ModuleNotFoundError. When trying to import something that doesn’t exist from a module that doesn’t exist, the result will be ImportError

IndexError

IndexError is caused when trying to get an index from a list or a tuple, the index is not found

For example

KeyError

Like IndexError, KeyError is triggered when trying to access a key without mapping, usually dict.

NameError

NameError is caused when you have referenced a variable, module, class, function or some other name that has been defined in your code. The Python document identifies when this exception is raised:

SyntaxError

missing “:”

TypeError

The TypeError is triggered when your code tries to do something with an object that doesn’t allow it, such as trying to add a string to an integer or calling len () on an object of that length. its defined.

ValueError

ValueError is invoked when the value of the object is incorrect. You can think of this as an elevated IndexError because the value of the index is the value in the range of the string, only ValueError is for the more general case.

3. Log 1 traceback like?

For example, there is a following code

Error encountered, quickly using try except to catch exceptions written to the log

Share the news now

Source : Viblo