Python Exception Handling Guide

People who write code often see exception handling as a necessary evil. But mastering the exception handling system in Python can improve the professional level of a programmer, make it more efficient. In this article, I will cover the following topics, the study of which will help everyone to unlock the potential of Python through a reasonable approach to exception handling:

  • What is exception handling?

  • The difference between the operator if and exception handling.

  • Partitioning else And finally block try-except to organize proper error handling.

  • Define custom exceptions.

  • Recommendations for handling exceptions.

What is exception handling?

Exception handling is the process of writing code to catch and handle errors or exceptions that may occur while executing a program. This allows developers to create reliable programs that continue to run even when unexpected events or errors occur. Without an exception handling system, this usually results in fatal crashes.

When exceptions occur, Python searches for a suitable exception handler. After that, if the handler is found, its code is executed, in which the appropriate actions are taken. This can be data logging, message output, an attempt to restore the program after an error. In general, we can say that exception handling helps to increase the reliability of Python applications, improves their support capabilities, and makes them easier to debug.

Differences between an if statement and exception handling

The main differences between the operator if and exception handling in Python stem from their goals and use cases.

Operator if is the basic building block of structured programming. This statement tests a condition and executes different blocks of code based on whether the condition being tested is true or false. Here is an example:

temperature = int(input("Please enter temperature in Fahrenheit: "))
if temperature > 100:
    print("Hot weather alert! Temperature exceeded 100°F.")
elif temperature >= 70:
    print("Warm day ahead, enjoy sunny skies.")
    print("Bundle up for chilly temperatures.")

Exception handling, on the other hand, plays an important role in writing robust and fault-tolerant programs. This role is revealed through dealing with unexpected events and errors that may occur during program execution.

Exceptions are used to signal problems and to identify sections of code that need improvement, debugging, or providing them with additional error checking mechanisms. Exceptions allow Python to deal gracefully with situations in which errors occur. In such situations, exceptions make it possible to continue running the script instead of abruptly stopping it.

Consider the following code, which shows an example of how you can implement exception handling and improve the potential failures associated with division by zero:

# Определение функции, которая пытается поделить число на ноль
def divide(x, y):
    result = x / y
    return result
# Вызов функции divide с передачей ей x=5 и y=0
result = divide(5, 0)
print(f"Result of dividing {x} by {y}: {result}")


Traceback (most recent call last):
  File "<stdin>", line 8, in <module>
ZeroDivisionError: division by zero attempted

After an exception has been generated, the program, before reaching the instruction printstops executing immediately.

The above exception can be handled by wrapping the function call divide to block try-except:

# Определение функции, которая пытается поделить число на ноль
def divide(x, y):
    result = x / y
    return result
# Вызов функции divide с передачей ей x=5 и y=0
    result = divide(5, 0)
    print(f"Result of dividing {x} by {y}: {result}")
except ZeroDivisionError:
    print("Cannot divide by zero.")


Cannot divide by zero.

By doing this, we handled the exception neatly ZeroDivisionErrorprevented the rest of the code from crashing due to an unhandled exception.

Details on other Python built-in exceptions can be found Here.

Using the else and finally sections of a try-except block to properly handle errors

When dealing with exceptions in Python, it is recommended to include in blocks try-except and section elseand section finally. Chapter else allows the programmer to customize what happens if no exceptions are thrown while executing code that is being protected from problems. A section finally allows you to ensure that some final operations, such as freeing resources, are mandatory, regardless of the fact that exceptions occur (Here And Here useful information about it).

For example, consider a situation where you need to read data from a file and perform some actions with this data. If an exception occurs while reading the file, the programmer may decide that it is necessary to log the error and stop further operations. But in any case, the file must be properly closed.

Partitioning else And finally allows you to do just that – process the data in the usual way if no exceptions occurred, or process any exceptions, but, no matter how the events develop, eventually close the file. Without these sections, the code would suffer vulnerabilities in the form of resource leaks or incomplete error handling. As a result, it turns out that else And finally play a critical role in building fault-tolerant and reliable programs.

    # Открытие файла в режиме чтения
    file = open("file.txt", "r")
    print("Successful opened the file")
except FileNotFoundError:
    # Обработка ошибки, возникающей в том случае, если файл не найден
    print("File Not Found Error: No such file or directory")
except PermissionError:
    # Обработка ошибок, связанных с разрешением на доступ к файлу
    print("Permission Denied Error: Access is denied")
    # Всё хорошо - сделать что-то с данными, прочитанными из файла
    content ='utf-8')
    processed_data = process_content(content)
# Прибираемся после себя даже в том случае, если выше возникло исключение

In this example, we first try to open a file file.txt for reading (in such a situation, you can use the expression with, which ensures that the file object is properly automatically closed upon completion). If errors occur during file I/O operations FileNotFoundError or PermissionError – the relevant sections are completed except. Here, for the sake of simplicity, we only display error messages on the screen and exit the program if the file is not found.

Otherwise, if the block try no exceptions were thrown, we continue processing the contents of the file in the branch else. And finally – “cleanup” is performed – the file is closed regardless of the occurrence of an exception. This provides a block finally (see details Here).

By adopting a structured approach to exception handling similar to the one above, you can keep your code well organized and readable. In this case, the code will be designed to deal with potential errors that may arise when interacting with external systems or input data.

Defining custom exceptions

In Python, you can define custom exceptions by subclassing the built-in class Exception or any other classes that are direct descendants Exception.

In order to define your own exception, you need to create a new class that is a descendant of one of the appropriate classes, and equip this class with attributes that correspond to the needs of the programmer. You can then use the new class in your own code, working with it in the same way as you work with the built-in exception classes.

Here is an example of defining a custom exception called InvalidEmailAddress:

class InvalidEmailAddress(ValueError):
    def __init__(self, message):
        self.msgfmt = message

This exception is a successor ValueError. Its constructor takes an optional argument message (by default it is set to invalid email address).

You can throw this exception if the program encounters an email address that has an incorrect format:

def send_email(address):
    if isinstance(address, str) == False:
        raise InvalidEmailAddress("Invalid email address")
# Отправка электронного письма

Now if the functions send_email() will be passed a string containing a malformed address, then instead of a standard error message TypeError, a pre-configured error message will be issued that clearly indicates the problem that has occurred. For example, it might look like this:

>>> send_email(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/path/to/project/", line 8, in send_email
    raise InvalidEmailAddress("Invalid email address")
InvalidEmailAddress: Invalid email address

Exception Handling Best Practices

Here are some tips related to error handling in Python:

  1. Design your code for bugs. Plan your code ahead of time for possible failures, and design your programs to handle these failures adequately. This means anticipating possible edge cases and implementing appropriate error handlers.

  2. Use meaningful error messages. Have the program print verbose error messages to the screen or log file to help users understand what went wrong and why. Avoid using generic error messages like Error occurred or Something bad happened. Instead, think about the user experience and show a message that gives advice on solving the problem or provides a link to the documentation. Try to strike a balance between displaying verbose messages and overloading the user interface with redundant data.

  3. Minimize Side Effects. Try to minimize the consequences of failed operations by isolating problematic sections of code with a construct try-finally or try using with. Make it so that after the execution of the code, whether it was successful or not, “cleanup” operations would be performed without fail.

  4. Test your code thoroughly. Ensure the correct behavior of error handlers in various application scenarios by subjecting the code to comprehensive testing.

  5. Refactor your code regularly. Refactor error-prone code snippets to improve their reliability and performance. Try to keep your codebase organized in a modular way, so that its individual parts are weakly dependent on each other. This allows independent parts of the code to evolve on their own without negatively affecting other parts of it.

  6. Log important events. Keep track of interesting events in your application by logging information about them to a log file or by printing them to the console. This will help you to identify problems at an early stage of their occurrence, without wasting time on a long analysis of a large number of unstructured logs.


Writing error handling code is an integral part of the software development industry, and Python development in particular. This allows developers to create more reliable and stable programs. By following industry standards and best practices for exception handling, a developer can reduce the amount of time it takes to debug code, can ensure that quality programs are written, and that programs are enjoyable for users to use.

Oh, and come to work with us? 🤗 💰

We are in doing high-frequency algorithmic trading since 2014. High-frequency trading is a continuous competition between the best programmers and mathematicians around the world. By joining us, you will become part of this exciting fight.

We offer interesting and challenging tasks in data analysis and low latency development for enthusiastic researchers and programmers. Flexible schedule and no bureaucracy, decisions are quickly made and implemented.

Now we are looking for plus developers, pythonists, data engineers and ml-risers.

Join our team.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *