Skip to content

Error Handling

Python raises exceptions when things go wrong. Catch them with try/except.

try:
1 / 0
except ZeroDivisionError as e:
print("Cannot divide by zero")
finally:
print("Done")

C# and JavaScript use try/catch blocks with similar semantics.

  1. Which block runs regardless of success or failure?
    Answer`finally`.
  2. What exception is thrown for invalid list access?
    Answer`IndexError`.