eolas/zk/Error_handling_in_Python.md

120 lines
3.2 KiB
Markdown
Raw Permalink Normal View History

2023-03-06 07:41:40 +00:00
---
tags: [python]
---
# Error handling and exceptions in Python
Errors can typically occur in the following scenarios:
- A file or resource that is referred to does not exist
- A network connection fails
- A module import error occurs
In these scenarios you would want to allow for error handling, so that you can
detect errors and respond appropriately.
2023-03-06 07:41:40 +00:00
## Difference between errors and exceptions.
2023-09-28 07:12:33 +01:00
Python distinguises between errors and exceptions.
2023-03-06 07:41:40 +00:00
- An error typically indicates a situration where something goes wrong before
the execution of the program begins.
2023-09-28 07:12:33 +01:00
- An exception arises during the execution of the program
2023-03-06 07:41:40 +00:00
In contrast to a error, when an exception occurs, the program doesn't
necessarily stop immediately. Instead, Python provides a way to handle the
exception, allowing you to potentially recover from it, or at least, to handle
it gracefully before the program stops.
2023-09-28 07:12:33 +01:00
However if you do not implement exception handling, the program will stop
immediately when an exception occurs, similar to a error.
2023-03-06 07:41:40 +00:00
## The Exception hierarchy
Errors and Exceptions are objects in Python and there is no real syntactic
distinction between the two since all errors and exceptions in herit from a base
exception class.
2023-03-06 07:41:40 +00:00
The root class is `BaseException` which all errors and exeptions extend as
subclasses as demonstrated by this diagram:
2023-03-06 07:41:40 +00:00
2024-02-16 16:14:01 +00:00
![](/img/python-exception-hierarchy.png)
2023-03-07 07:39:05 +00:00
## Exception syntax
2023-09-28 07:12:33 +01:00
### Difference between `raise` and `except`:
- `raise` is used to explicitly trigger an exception - it means that you are
signalling that an exception condition _has_ occured in your program.
- `except` is used in conjunction with `try`blocks to catch and handle
exceptions. Here you are saying "I think this _might_ cause an exception, so
let's be prepared to handle it".
2023-03-07 07:39:05 +00:00
2023-09-28 07:12:33 +01:00
Exaple of `raise`:
```py
x = -10
if x < 0:
raise ValueError("The value should not be negative!")
```
Example of `except`:
```py
try:
result = 10 / 0
except ZeroDivisionError:
print("You can't divide by zero!")
```
### Scaffolding exception handling
There is a general procedure for handling exceptions denoted by certain
keywords:
2023-03-07 07:39:05 +00:00
- `try`
- The process you want to run
- `except`
- The errors that could occur. You can have multiple `except` clauses for
different exceptions
2023-03-07 07:39:05 +00:00
- `else`
- Some code you want to run after each of the `except` clauses have run
- It must be written after the `except` clauses
- It runs **if and only if** no exceptions were raised
- If `try` succeeds or an exception is thrown, `else` will not run
- `finally`
- What you want to run at the end of the `try, except, else` sequence
```py
try
run_calculation(7)
except ZeroDivisionError:
print('something')
except IndexError:
print('something')
except FileNotFoundError:
print('something')
except Exception:
print('something')
else
# Do something after the exception blocks
finally
# Do concluding action
```
2023-09-28 07:12:33 +01:00
## Custom exceptions
You can create your own custom exceptions by creating a class that inherits from
the `Exception` class.
2023-09-28 07:12:33 +01:00
```py
class CustomError(Exception):
pass
try:
raise CustomError("This is a custom exception!")
except CustomError as e:
print(f"Caught an exception: {e}")
```