Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing
Back
  1. Home
  2. Interview prep
  3. What is exception handling?

Python · Fundamentals & Gotchas

What is exception handling?

Mediumpython-38
exceptionstryexceptfinallyraise

Question

What is exception handling in Python?

Solution

Exception handling is how you detect and recover from runtime errors using try / except / else / finally, instead of crashing the process.

  • try: code that might fail
  • except: handlers for specific exception types (prefer narrow types)
  • else: runs if try had no exception
  • finally: always runs (cleanup)
  • raise: throw / rethrow an exception
def parse_int(raw: str) -> int:
    try:
        value = int(raw)
    except ValueError as exc:
        raise ValueError(f"not an int: {raw!r}") from exc
    else:
        return value
    finally:
        print("attempt finished")

try:
    print(parse_int("42"))
    print(parse_int("x"))
except ValueError as exc:
    print("handled:", exc)

# Catch multiple
try:
    ...
except (KeyError, IndexError) as exc:
    ...

# Bare except / except Exception: use sparingly; can hide bugs

Best practices

  • Catch the narrowest exception you can handle.
  • Use raise ... from exc to preserve chains.
  • Prefer context managers for resource cleanup over only finally blocks when both apply.
  • Do not use exceptions for ordinary control flow in hot paths.

🎯 Put this concept into practice

Solidify this answer with real hands-on interview drills in the browser studio.

Open related drill →
PreviousNext