Errors are the bane of a programmer’s existence. You write an awesome piece of code, are ready to execute it and build a powerful machine learning model, and then poof. Python throws up an unexpected error, ending your hope of quick code execution. Show
Every single one of us has faced this issue and emerged from it a better programmer. Dealing with bugs and errors is what builds our confidence in the long run and teaches us valuable lessons along the way. We have some rules while writing programs in any programming language, such as not using a space when defining a variable name, adding a colon (:) after the if statement, and so on. If we don’t follow these rules, we run into Syntax Errors and our program refuses to execute until we squash those errors. But there are occasions when the program is syntactically correct and it still throws up an error when we try to execute the program. What’s going on here? Well, these errors detected during the execution are called exceptions. And dealing with these errors is called exception handling. We’ll be talking all about exception handling in Python here! Table of Contents
Advantages of Exception HandlingWhy should you learn exception handling? Let me answer that using a two-pronged argument:
Common ExceptionsHere’s a list of the common exception you’ll come across in Python:
You can read about more common exceptions here. Exception Handling in Python: Try and Except StatementLet’s define a function to divide two numbers a and b. It will work fine if the value of b is non-zero but it will generate an error if the value of b is zero: We can handle this using the try and except statement. First, the try clause will be executed which is the statements between the try and except keywords. If no exception occurs, the except clause will be skipped. On the other hand, if an exception occurs during the execution of the try clause, then the rest of the try statements will be skipped: Exception Handling in Python: Else StatementIn Python, we can also instruct a program to execute certain lines of code if no exception occurs using the else clause. Now, if no exception occurs in the above code, we want to print “No Error occurred!!”. Let’s see how to do this: Exception Handling in Python: Finally StatementNow, what if we need some sort of action that will execute whether the error occurred or not (like maintaining logs). For this, we have the finally clause in Python. It will always get executed whether the program gets any of the exceptions or not. We will see how we can use the finally clause to write the logs later in this article. Now, in the above example, I want to print the value of a and b after every execution regardless of whether the error occurred or not. Let’s see how to do that: Real-life Use Case of Exception Handling in PythonSo far, we have seen exception handling on some random data. How about turning the lever up a notch and understanding this using a real-life example? We have data that contains the details of employees like their education, age, number of trainings undertaken, etc. The data is divided into multiple directories region-wise. The details of employees belonging to the same region are stored in the same file. Now, our task is to read all the files and concatenate them to form a single file. Let’s start by importing some of the required libraries. Get the directory structureTo view the directory structure, we will use the glob library and to read the CSV files, we will use the Pandas library: View the directory structure using the glob.glob function and the path of the target directory. You can download the directory structure here. We can see that folder names are represented as some numbers and in the next step will go through each of the directories and see the files present: In each of the folders, there is a CSV file present that contains the details of the employees of that particular region. You can open and view any CSV file. Below is the image of how data looks like in the region_1.csv file. It contains details of employees belongning to region 1: Read the filesNow, we know that there is a pattern in the directory and filename structure. In the directory n, there is a CSV file name region_n present. So now we will try to read all these files using a loop. We know that the maximum number is 34 so we will use a for loop to iterate and read the files in order: You can see that the file region_7 is not present. So, one of the simpler ways to deal with this is to put an if condition in the program – if the directory name is 7 then skip reading from that file. But what if we have to read thousands of files together? It would be a tedious task to update the if condition every time we get an error. Here, we will use the try and except statement to handle the errors. If there is any exception during the run time while reading any file, we will just skip that step and continue reading the next folder. We will print the file name with “File not found!!” if the error is FileNotFoundError and print the file name with “Another Error!!” if any other error occurs. We can see that we got another error in file number 32. Let’s try to read this file separately: There are some issues with File 32’s format. If you open the file region_32.csv, you will see that there are some comments added on top of the file and we can read that file using the skiprows parameter: Let’s see how to handle this. Try, Except and Else StatementsWe will create two boolean variables – parse_error, file_not_found – and initialize both of them as False at the start of each iteration. So, if we get FileNotFoundError, then we’ll set file_not_found as True. Then, we will print that particular file as missing and skip that iteration. If we get ParserError, then we’ll set parse_error as True and print that the particular file has an incorrect format and read the file again using the skiprows parameter. Now, if no exception occurs, then the code under the else statement will execute. In the else statement, we will append the data frame to the data frame list: Try, Except, Else and Finally StatementLet’s say you want to create a log file to keep a track of which files are correct and which ones have errors. This is one use case of the finally statement. Whether you getthe error or not, the finally statement will execute. So in the finally clause, we will write to the file regarding the status of the file at each iteration: End NotesI use exception handling while scraping data from multiple webpages. I’d love to know how you use exception handling so comment down below with your thoughts and share them with the community. If you found this article informative, then please share it with your friends and comment below your queries and feedback. I have listed some amazing articles related to Python and data science below for your reference: What are the different ways to handle exceptions in Python?In Python, exceptions can be handled using a try statement. The critical operation which can raise an exception is placed inside the try clause. The code that handles the exceptions is written in the except clause. We can thus choose what operations to perform once we have caught the exception.
What happens after exception Python?If any exception occurs, the try clause will be skipped and except clause will run. If any exception occurs, but the except clause within the code doesn't handle it, it is passed on to the outer try statements. If the exception is left unhandled, then the execution stops.
What is the best way to overcome the exception?Use try/catch/finally blocks to recover from errors or release resources.. Handle common conditions without throwing exceptions.. Design classes so that exceptions can be avoided.. Throw exceptions instead of returning an error code.. Use the predefined .NET exception types.. End exception class names with the word Exception.. How are the exceptions handled in scripting?The except block is executed when an exception occurs within the try block. The optional else block is executed only if there were no exceptions after try and before finally . The finally block contains instructions that are always executed at the end, regardless of whether exceptions occurred in the try block.
|