26.6 C
Wednesday, July 28, 2021

How to Catch All Exceptions in Python – Linux Hint

Must read

Programmers frequently classify exceptions as code faults that result in errors whenever the program is executed. Exceptions represent errors that take place as a result of a script. Some programmers still can’t tell the difference between mistakes as well as exceptions. The majority of such errors seem logical. In Python, exceptions are caught using straightforward logic. Whenever a Python interpreter encounters an exception, it terminates the workflow. It is dealt with by going through the calling procedure. The software will crash if this is not done. To distinguish among mistakes and exceptions, there have been two categories of mistakes to consider:

  • Syntax errors
  • Logical errors (Exceptions)

Syntax Error

To see the working of logical error’s we have to get through the example of syntax error first. Open the shell terminal using “Ctrl+Alt+T” from the keyboard. After opening the shell, we will create a new python file. To create a file, write the below-stated query shown in the image as it is.

Let’s open the file “test.py” from your local home directory by double-tapping on it. After the file has been opened, write out the below code in it as it is. You have to understand this code first. We have added python-support to it. After that, we have initialized a dictionary variable “ages” to add two keys with two values. We have not added a comma between both the keys. This will be our syntax error on runtime.  After that, we have printed the key “Aqsa” and its value from the dictionary. Save your file with Ctrl+S, and click on the cross button to leave it.

Let’s run our file by python3 version as below. The output image shows the Syntax Error saying: invalid syntax because the comma is missing from the statement. You can see there is no logical error.

Let’s just quickly update the code by adding a comma in the statement to prevent syntax errors. Save this code and close it via cross sign from the right side of the window.

Let’s run our file again to see the result by python3 version. The output of the snapshot below is showing the correct output with no syntax or logical errors.

Logical Errors

Logical errors are those exceptions that occur at a run time due to some logical error in our code. At the start of this guide, we have mentioned catching all the exceptions at the same time. But to understand that concept, we need first quickly to look at catching one exception at a time. Hence, open the shell terminal and convert it to python3 code format to use python on it using the below query as shown in the image.

Now the terminal is ready to use for coding. Add some statement that shows some logical error in it. Let say we have added “2/0,” which is not possible in mathematics. So this should throw some logical error. Enter it on shell and press Enter. The output shows the “ZeroDivisionError” because anything divided by zero cannot return an answer.

Let’s take another example of catching a single exception on the terminal. Open a file that is not even created or found in the current local directory of Linux as below. It will throw a “FileNotFoundError” exception at run time because there is no such file in our system.

Example: Catch-All Exceptions

Finally! It’s time to get all exceptions in one place. Hence, open the python file test.py from your current home directory of the Linux system. Now write the displayed below code. This code contains a list “l” with different sorts of elements in it.  We have used a “for” loop to get each value from the list and apply a try statement to it. The try statement will first print that element from the list. The next line will convert that element to integer-type first and then divide 1 with that converted element. After this, we have used the except statement that will print the exception name that occurred due to the “try” statement using a sys.esc_info() method. Another string will be printed out, and then after the “except” statement’s the body, we have initialized a print statement to print the result of division that has been done to try the statement. Press Ctrl+S to save your file and hit on the cross button to close it quickly. Now open the terminal to see the working of our code.

Execute the query below to run this file. The output shows the result of the print statement “The result is  “used in the “try” statement for each list element. As the first two elements of the list are string and multitype variables, division cannot occur. This leads to different errors. The first value “except” statement has been executed and shows the names of errors using the sys.exc_info() method. Due to the exception, the third print statement from the “except” clause will be executed for both the first two elements of the list. But you can see in the output that the result for the third value doesn’t throw any exception. This is because the third element of the list is an integer, and it can be divided by 1 perfectly. The except statement didn’t execute in this case; thus, the control has been given to the outermost print statement, and it outputs the resultant value generated by the division.


We have covered syntax and logical errors in this article. Most importantly, we have discussed the method to catch all exceptions in our article via the “sys.exc_info()” method usage.

Source link

- Advertisement -

More articles


Please enter your comment!
Please enter your name here

- Advertisement -

Latest article