capypad
0 day streak
python / beginner
Snippet

Basic Exception Handling

The try-except block is used to catch and handle exceptions, preventing the program from crashing when an error occurs.

snippet.py
python
1
2
3
4
try:
result = 10 / 0
except ZeroDivisionError:
print('You cannot divide by zero!')
Breakdown
1
try:
Starts a block of code that might raise an error.
2
except ZeroDivisionError:
Specifies which error to catch and provides a block of code to handle it safely.