AtomLearn
DashboardGoalsGraphAchievementsReviewSign In
DashboardPython CoreExceptions & Error Handling
Python CoreNot Started

Exceptions & Error Handling

try/except/finally, custom exceptions, raising errors

0%

Knowledge Debt detected

You can study this freely — but your score may plateau if these foundations have gaps. The Mastery badge requires them to be solid.

Explanation

Exceptions are errors that occur during execution. Python uses try/except to handle them gracefully.

python
try:
    result = 10 / 0
except ZeroDivisionError:
    print("Cannot divide by zero!")
finally:
    print("This always runs")

raise lets you throw your own exceptions: ``python def withdraw(amount): if amount < 0: raise ValueError("Amount must be positive")

Custom exceptions extend Exception: ``python class InsufficientFundsError(Exception): pass

This is essential for Flask — HTTP errors are exceptions in Flask.

Examples

Multiple except clauses

Handle different error types separately

try:
    data = int(input("Enter number: "))
    result = 100 / data
except ValueError:
    print("Not a valid number")
except ZeroDivisionError:
    print("Cannot divide by zero")
except Exception as e:
    print(f"Unexpected error: {e}")

Next in Python Core

Decorators

Continue