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.
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.
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
Always catch the most specific exception type you expect.