Fundamentals 6 min read

Python Error Handling and Debugging Techniques

This article explains common Python runtime errors, demonstrates how to read traceback information, introduces the try‑except‑finally construct, and covers practical debugging methods such as print/assert statements and the built‑in pdb module with example code snippets.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python Error Handling and Debugging Techniques

During program execution, various errors can occur due to logical oversights or unexpected user actions. When Python prints an error to the console, the traceback shows the sequence of calls leading to the failure.

<code>def test(name):
    return int(name)

test("cbekd")
</code>

Running the above code produces a ValueError with a traceback that pinpoints the exact line where the conversion failed.

<code>Traceback (most recent call last):
  File "test.py", line 3, in <module>
    test()
  File "test.py", line 2, in test
    return int("cbekd")
ValueError: invalid literal for int() with base 10: 'cbekd'
</code>

To handle such errors gracefully, Python provides the try...except...finally structure. Code inside try is executed, and if an exception occurs, control jumps to the matching except block; the finally block runs regardless of whether an exception was raised.

<code>try:
    print("try...before...")
    int("cbekd")
    print("try...after...")
except ValueError as e:
    print("except...", "detail:", e)
finally:
    print("finally...")
</code>

The output demonstrates that execution stops before the second print inside try , jumps to except , and then runs finally . Multiple except clauses can handle different exception types, and except BaseException catches any exception.

<code>try...before...
except... detail: invalid literal for int() with base 10: 'cbekd'
finally...
</code>

When the error location is known but the root cause is unclear, simple debugging techniques like inserting print() or assert statements can reveal variable values at each step, a method often called "breakpoint debugging".

Python's built‑in pdb module offers interactive debugging with commands to set breakpoints, step through code, and inspect state.

<code>import pdb

def test(name):
    pdb.set_trace()
    return int(name)

test("cbekd")
</code>

Running the script stops at pdb.set_trace() , allowing commands such as pp name to print the variable and n to step to the next line, where the ValueError is raised.

<code>> test.py(5)test()
-> return int(name)
(Pdb) pp name
'cbekd'
(Pdb) n
ValueError: invalid literal for int() with base 10: 'cbekd'
</code>

Understanding traceback information and using these error‑handling and debugging tools enables developers to locate and fix bugs efficiently.

debuggingpythonerror handlingExceptiontry-except
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.