Python Traceback

In this tutorial, we are going to learn about traceback in Python. And in the end, we will also see an example of this topic. So let’s start-

Traceback in Python:

Python mainly prints a traceback when there is some exception (An event that occurs during the execution of the program to dispute the flow of branch of instruction) written by us. But it is really hard to understand for someone who is seeing the exception for the first time or doesn’t about what exception is. But if someone can understand the exception at his / her own, he/she can surely fix the dispute as Python gives us enough information to understand why the exception is raised or what to do to solve the exception raised in our code.  If you can understand the traceback in Python, you can become a good coder in Python.

Definition: A traceback report is a report shown by the compiler which shows some exceptions made by coder and easily detectable. When our program has some exception Python prints the information to let the user know about what went wrong. The following is an exception where the misspelled exception is shown.

def call(person_name):
    print('Hello, ' + personname)


Here we are creating a function call and giving the argument as person_name. But when we’re using the print statement we have passed personname. Here we have looked over  ‘_’. And as a result of this in print function the print function cannot identify the word personname and shows that the variable is not used.

As the output of this code, it will show the exception like this:

Traceback (most recent call last):
File "<string>", line 6, in <module>
File "<string>", line 4, in call
NameError: name 'personname' is not defined

Now to read this in the last line of the report it clearly shows that NameError that ‘personname’ is not defined. In order to fix this, we have to look the line no 4 and 6 and also in the traceback report the line number is already mentioned.

So, we have learned the traceback in Python. Thank you.

Also read: How to catch HTTP 404 error in Python

Leave a Reply

Your email address will not be published. Required fields are marked *