How to customize exceptions in python

How to customize exceptions in python. Cover

How to customize exceptions in python

In this tutorial we will look at the purpose of exception, types of exceptions, why it is important to include exceptions in your python programs and where in a python program you can include exceptions.

In order to get practical understanding of how to use exceptions, we will build a python program to compute hash values of files based on md5 or sha256 algorithm.  We will implement custom  exceptions to deal with unexpected happenings such as empty input from users to prevent program from exiting abruptly.

NB: Exception and Error in this context are interchangeable.

Requirements:

You need to have python version 3.7 on your local host to run this program. You can check here to find out to install python version 3.7 .

What is an Exception?

Exception occurs when something that is expected to happen does not happen or something that is not expected happens.  There are certain exceptions or errors that we can handle such as type errors and those we can’t easily handle such as out of memory error.

For instance, let’s assume we have built a program in python which allows users to connect to soshace.com to download articles or tutorials as shown below:

There is a possibility that the code above can end abruptly if there is no network connectivity or network latency is low.  In this case, we expected the user to connect and get the specified resource but because of connectivity problem, it can’t.

In another instance, the code below can return a type error if a user enters any other value apart from an integer because the index method for the list data type accept only integers.

If you run this code via the python interpreter, it shows the following as type error because list indices do not support strings.

We can handle errors like those above using exceptions: whether in-built or user-defined.

Hierarchy of Exceptions

There are various classes of exception. All of these classes, in-built such as TypeError, IndexError and custom or user-defined, inherit directly from the BaseException class or from sub-classes of the BaseException class such as the Exception class.

However PSF recommends we inherit the Exception class instead of the Base Exception class. You can check here for a list of all exceptions in python.

The BaseException class is the super class of all classes or kinds of exceptions in python.

For instance, the code below technically shows how the InvalidAuth exception inherits from the Exception class.

NB: Inheritance in python allows a class to inherit attributes or behaviours otherwise known as methods from another class.

In this context, the InvalidAuth class becomes a subclass of Exception. So the Exception class is the super class of the InvalidAuth class.

You can check here for detailed information on inheritance, classes and super class in python 3.

Types of Exceptions:

In python there are two main types of exceptions: in-built and user-defined exceptions.

In-Built Exceptions:

Some instances of in-built exceptions in python are as follows:

KeyError:

This type of error or exception usually shows up or occurs when the specified key is not present in a data structure such as a dictionary.

The code below demonstrates how a program can terminate abruptly due to a key error if we decided to search for a non-existent key.

 

IndexError:

This type of error occurs when index accessed is out of range. This error shows up when a program make use of a list or tuple data structure to store data.

The code below demonstrates how a program can terminate abruptly due to an Index error.

 

NameError:

This type of error shows up if python interpreter tried to use a variable or a function but it has not been defined yet.   The code below shows how a name error occurs.

 

AttributeError: 

Attribute error occurs when you try to access or contact an object’s attribute which does not yet exist.  The code below shows how attribute error occurs.

Apart from these errors or exceptions, there are other errors such as Timeout, TooManyRedirects, and ConnectionError. You can check here for additional in-built exceptions.

User-Defined Exceptions:

These type of exceptions are usually implemented by developers who need specific type of exception to handle errors.

Moreover, instead of also allowing in-built exception to output traceback when an exception is raised, we can customize it in our own way in this article. These type of exceptions inherit directly from the Exception class likewise in-built exceptions.

For instance, we can create an exception such as the one below to handle or catch error such as when a user tells the program to search for a file but that file is not available on the system.

Why it is important to include exceptions in your program:

The following shows why it is important to include exceptions in your python program to catch errors:

  1. It is unprofessional to deploy a program without a way to catch exceptions to production environment because it can dent a company’s reputation since the program can terminate abruptly or crash.
  2. The possibility of a company losing many customers due to the fact that it crashes often.
  3. Traceback printed by python can be a bit overwhelming.

Where in a python program can you include exceptions:

In this section, we will build a python tool to compute hash values of files. In addition, we will prevent the program from exiting abruptly by catching exceptions and customizing output to users.

On the terminal create a file using the following command:

Open the cmd.py file with vim editor and paste the following import statements:

Now save and close the cmd.py file using your file editor.

The import statements are as follows:

  • import hashlib:  This imports hashlib module otherwise known as hashing library. hashlib comes with one-way hashing algorithm such as SHA-256 family algorithms made up of SHA-1 and SHA-2.
  • import os:  This os module provides functions to interact with filesystem.
  • import shutil: This module provides functions we can apply on a collection of files.
  • import sys:  This module manipulates the python runtime environment. 

Now copy and paste the following code to the cmd.py file as shown below:

Then save and close the cmd.py file using your file editor.

The above code is made up of four functions respectively. These functions are described as follows:

  • create_files function:   This function allows a user to create files.
  • write_file function:  This function allows users to write to a file inside the program’s directory.  
  • hash_files function:  This function computes hash values of a file based on a user’s selected hashing algorithm.
  • make_dir function:  This function allows a user to create a directory inside the program’s root directory.

In addition,  we decided to catch errors and customize it in our own way during a user’s input such when creating a file or a directory.

On the terminal, execute the following command to run the program:

The program then prompts you to choose which function to run. You can choose to run any of the functions but initially you need to run the create_files and the write_file functions first.

Then afterwards, you can call the hash_files function to compute hash values for files created.

Conclusion:

Although in this article, we made use of the try except syntax to handle both expected and unexpected happenings, we can also make use of the if else syntax in python to handle exceptions by controlling the flow of the program with regards to user’s input.

So It is not always necessary to use the try except syntax to catch errors in the program. You can also deal with expected errors in a program using the if else syntax.

About the author

Stay Informed

It's important to keep up
with industry - subscribe!

Stay Informed

Looks good!
Please enter the correct name.
Please enter the correct email.
Looks good!

Related articles

27.07.2023

Creating Our Own Chat GPT

In June, OpenAI announced that third-party applications' APIs can be passed into the GPT model, opening up a wide range of possibilities for creating ...

12.06.2023

The Ultimate Guide to Pip

Developers may quickly and easily install Python packages from the Python Package Index (PyPI) and other package indexes by using Pip. Pip ...

Building a serverless web application with Python and AWS Lambda

AWS Lambda is a serverless computing solution that enables you to run code without the need for server provisioning or management. It automatically ...

No comments yet

Sign in

Forgot password?

Or use a social network account

 

By Signing In \ Signing Up, you agree to our privacy policy

Password recovery

You can also try to

Or use a social network account

 

By Signing In \ Signing Up, you agree to our privacy policy