try — Easy Examples
Starts a block of code that will be monitored for exceptions
Basic error handling
Catching exceptions with try/except.
python
try: result = 10 / 0 except ZeroDivisionError: print("Cannot divide by zero!") try: number = int("hello") except ValueError as e: print(f"Invalid input: {e}") print("Program continues normally")
Expected Output
Cannot divide by zero! Invalid input: invalid literal for int() with base 10: 'hello' Program continues normally
try/except prevents your program from crashing when errors occur. Always catch specific exceptions, not bare except.
Want to try these examples interactively?
Open Easy Playground