Skip to main content
🔒 Preview mode. The first 15 Foundations lessons are free; this one is Pro. Start a 7-day trial to unlock the editor, AI hints and the rest of the curriculum. Card required, cancel any time in Dashboard.Start 7-day trial →
← CoursesSenior Deep-DivesModule 9 · Error handling depthtraceback.TracebackException — exceptions as datapredict138 / 161
+150 XP
Task
📝 **Task:** Predict the 5-line output. A function `divide(a, b)` is called with b=0, raising ZeroDivisionError. Inside the except block, we build a TracebackException and print its exc_type name, frame count, last frame name, total formatted-line count, and the first formatted line. 📋 Implement the function above. Tests run automatically. 💡 **Hint:** Re-read the theory if you get stuck.
Predict the output

Read the code carefully

import traceback


def divide(a: int, b: int) -> float:
    return a / b


try:
    divide(10, 0)
except Exception as e:
    te = traceback.TracebackException.from_exception(e)
    print(f"exc_type: {type(e).__name__}")
    print(f"frames: {len(te.stack)}")
    print(f"last frame: {te.stack[-1].name}")
    lines = list(te.format())
    print(f"line count: {len(lines)}")
    print(f"first line: {lines[0].rstrip()}")

What will the program print? Write here:

💬 Discussion

Be the first to ask a question or share a tip.
Sign in to join the discussion. Reading is free.
Loading discussion…