try

KeywordPython 2.0+Beginner

Starts a block of code that will be monitored for exceptions

Quick Info

Documentation
Official Docs
Python Version
2.0+

Learn by Difficulty

Quick Example

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")

try/except prevents your program from crashing when errors occur. Always catch specific exceptions, not bare except.

Try in Playground

Tags

languagesyntaxcoreerror-handlingexception

Related Items