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
1
2
3
4
try:result = 10 / 0except 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.