Skip to content
Learn Netverks

Lesson

Step 17/36 47% through track

errors-exceptions

Errors and exceptions

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Errors and exceptions: the syntax, patterns, and safety habits you need before advancing in Python.

EAFP (try/except) is idiomatic—know when not to blanket-catch Exception.

You will apply Errors and exceptions in contexts like: Scripts, Django/FastAPI apps, notebooks, and glue code between systems.

Write Python 3 in the editor and click Run on server—the dev runner executes your script with print() for output; stdlib only in playground snippets (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Python raises exceptions for errors—try/except/else/finally handles them. Unlike some languages, exceptions are the normal control path for many error cases, not only catastrophes.

try / except / else / finally

try:
    value = int("42")
except ValueError as e:
    print("bad input", e)
else:
    print("parsed", value)
finally:
    print("cleanup always runs")

Raising and custom exceptions

raise ValueError("expected positive number")

Catch specific exceptions first—avoid bare except: that swallows KeyboardInterrupt. Use raise to re-raise and preserve traceback.

Important interview questions and answers

  1. Q: else on try?
    A: Runs only if no exception occurred—good for code that should not run if try failed.
  2. Q: EAFP vs LBYL?
    A: "Easier to ask forgiveness"—try/except; "Look before you leap"—check conditions first. Python culture favors EAFP for many cases.

Self-check

  1. When does finally run?
  2. Why avoid bare except?

Tip: Catch specific exceptions—bare except: hides bugs and KeyboardInterrupt.

Interview prep

else on try?

Runs if no exception in try—keeps success path separate from except blocks.

EAFP vs LBYL?

Python favors try/except (easier to ask forgiveness) for many error cases vs pre-checking everything.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • EAFP vs LBYL?
  • finally always?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump