SyntaxStudy
Sign Up
Python Beginner 3 min read

Try/Except Basics

Try/Except Basics

Python uses try and except blocks to handle runtime errors gracefully instead of crashing.

Code that might fail goes in the try block. If an exception occurs, the matching except block runs.

Example
try:
    result = 10 / 0
except ZeroDivisionError:
    print("Cannot divide by zero")
Pro Tip

Always catch the most specific exception type you expect.