Error handling in Python is achieved by coding block of scripts, which might cause exceptions, inside a ‘try’ block. If an error occurs during the execution of this block, the control would shift to the corresponding ‘except’ block. Here, you specify which types of exceptions you would like to catch and address.
Here’s an example:
```
try:
# Block of code that might cause exceptions
x = 1 / 0
except ZeroDivisionError:
# What to do when a ZeroDivisionError occurs, like print an error message
print(“You can’t divide by zero! Please try again.”)
```
In the above example, when a `ZeroDivisionError` occurs due to the operation `1 / 0`, Python would raise an error and stop the execution. However, because we’ve surrounded this line of code within a `try` block, Python instead passes the control to the `except` block which specifies how the script should respond to this type of error.
More broadly, the standard way to use `try` and `except` is :
```
try:
# Block of code to try to execute
except ExceptionType1:
# How to handle ExceptionType1
except ExceptionType2:
# How to handle ExceptionType2
except (ExceptionType3, ExceptionType4):
# How to handle multiple types of exceptions
except:
# If none of the above exceptions occur, this block will execute
else:
# If no exception occurs, this block will execute
finally:
# This block always executes, whether an exception occurred or not.
```
Note: Python executes code following `else` only if no exceptions were raised. Code inside `finally` will always execute, regardless of whether an exception was raised or not. It is typically used to release external resources.