Skip to content

Exceptions

Exceptions are a mechanism for handling errors in Python (and most programming language). When an error occurs when running your code, Python raises an exception. There are multiple types of exceptions. Each exception has a name and a message.

If the exception is not caught, the program will terminate immediately, raising the corresponding exception.

src.intermediate.exceptions.exception_uncontrolled()

Method that raises an exception that is not handled correctly.

Raises:

Type Description
TypeError

cannot sum int&string types

Source code in src/intermediate/exceptions/exceptions.py
15
16
17
18
19
20
21
22
23
def exception_uncontrolled() -> None:
    """Method that raises an exception that is not handled correctly.

    Raises:
        TypeError: cannot sum int&string types
    """
    number = 1
    char = "a"
    number + char  # raise TypeError, cannot sum int&string types

Controlling exceptions

Exceptions can be caught and handled using a try block. You can catch the exception, do something (like logging), and continue running the program.

src.intermediate.exceptions.exception_controlled()

Method that control the exception, no raise.

Source code in src/intermediate/exceptions/exceptions.py
26
27
28
29
30
31
32
33
34
def exception_controlled() -> None:
    """Method that control the exception, no raise."""
    number = 1
    char = "a"
    try:
        number + char  # raise TypeError, cannot sum int&string types
    except TypeError:
        logger.warning("Cannot sum int + string. Continue.")
        pass

You can catch the exception, logging and raise the same exception to terminate the execution.

src.intermediate.exceptions.exception_controlled_raise_exception()

Method that control the exception, raising the same exception.

Raises:

Type Description
TypeError

cannot sum int&string types

Source code in src/intermediate/exceptions/exceptions.py
37
38
39
40
41
42
43
44
45
46
47
48
49
def exception_controlled_raise_exception() -> None:
    """Method that control the exception, raising the same exception.

    Raises:
        TypeError: cannot sum int&string types
    """
    number = 1
    char = "a"
    try:
        number + char  # raise TypeError, cannot sum int&string types
    except TypeError as exc:
        logger.error("Cannot sum int + string. Raising TypeError.")
        raise exc

Similar way, ou can catch the exception, logging and raise another type of exception. You can terminate the execution of a running program by raising an exception at any time.

src.intermediate.exceptions.exception_controlled_raise_custom_exception()

Method that control the exception, raising custom exception.

Raises:

Type Description
TypeError

cannot sum int&string types

Source code in src/intermediate/exceptions/exceptions.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def exception_controlled_raise_custom_exception() -> None:
    """Method that control the exception, raising custom exception.

    Raises:
        TypeError: cannot sum int&string types
    """
    number = 1
    char = "a"
    try:
        number + char  # raise TypeError, cannot sum int&string types
    except TypeError as exc:
        logger.error("Cannot sum int + string. Raising CustomError.")
        raise CustomError(
            message="Controlled TypeError",
            exception=exc,
        )

With the finally block, you can run code that will always run, regardless if the code in the try block raises an exception. It will be always executed.

src.intermediate.exceptions.exception_controlled_raise_custom_exception()

Method that control the exception, raising custom exception.

Raises:

Type Description
TypeError

cannot sum int&string types

Source code in src/intermediate/exceptions/exceptions.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def exception_controlled_raise_custom_exception() -> None:
    """Method that control the exception, raising custom exception.

    Raises:
        TypeError: cannot sum int&string types
    """
    number = 1
    char = "a"
    try:
        number + char  # raise TypeError, cannot sum int&string types
    except TypeError as exc:
        logger.error("Cannot sum int + string. Raising CustomError.")
        raise CustomError(
            message="Controlled TypeError",
            exception=exc,
        )

References