Introduction
This is an article focusing on how to perform error handling in Python programming language. Actually, there is already an article discussing about ‘Error in Python’ exist as an article in this link. In that article, there are descriptions about type of errors available in Python programming language. So, this article will discuss it further as part on the content for describing how to handle those errors in Python programming language. Actually, in case of an error appear in the running program, the program itself will be forced to stop or it must be terminated. According to the type of the error, there is two types of error appear when the running program is facing an errors. The first one is an error syntax and the second one is an error exception.
Syntax Error Handling in Python
A syntax error is the most common and general error to appear in a running program. In this case, handling the error is very easy. Just check for the miss-typed reserved keyword or any colons which is not exist. Without further explanation, the following is the description of the error in the form of an example and also how to handle that type of error below :
Miss-typed Syntax Error Handling in Python
Below is an example for the miss-typed syntax error appear when performing loop process. Instead of typing ‘for’ reserved keyword to perform loop, it ends with ‘fore’ as follows :
Microsoft Windows [Version 10.0.22000.856] (c) Microsoft Corporation. All rights reserved. C:\Users\Personal>python Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>fore x in range(0,6): File "<stdin>", line 1 fore x in range(0,6): ^ SyntaxError: invalid syntax >>>
As it exist above, the SyntaxError is pointing at the ‘fore’ reserved keyword .So, it is very easy to handle it. Just revise and correct it as in the following execution :
C:\Users\Personal>python Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> for x in range(0,6): ... print("Number : ",x+1) ... Number : 1 Number : 2 Number : 3 Number : 4 Number : 5 Number : 6 >>>
Incomplete Syntax Error Handling in Python
In term of the incomplete syntax error handling, before going on further, below is the example of the execution of that incomplete syntax error :
Microsoft Windows [Version 10.0.22000.856] (c) Microsoft Corporation. All rights reserved. C:\Users\Personal>python Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> for x in range(0,6) File "", line 1 for x in range(0,6) ^ SyntaxError: expected ':' >>>
As it exist above, the SyntaxError is pointing at the end of the for statement. It is expecting ‘:’ which is pointing that the for loop format is incomplete. So, it is very easy to handle that kind of error message. Since the error message is also hinting the solution, just revise the source code by adding the expected character. In this case, it is adding ‘:’ at the end of the ‘for’ statement as in the following execution :
C:\Users\Personal>python Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> for x in range(0,6): ... print("Number : ",x+1) ... Number : 1 Number : 2 Number : 3 Number : 4 Number : 5 Number : 6 >>>