Python

Python Print Exception Message

In programming, errors are generally the same for all languages. Python came up with the idea to print out the error message as a normal text in the output screen and avoid your program to stop immediately upon encountering errors. For this, python utilizes the “try-except” statement in its programs. So, let’s take a look at it using the Spyder3 Python tool.

Example 01:

Let’s have a look at the first exception to be printed out in the Python terminal as a simple message without halting the program. So, we have been using the most well-known “try-except” statement of a python language within our code. In this program, we will be putting the code with an error in the “try” part. We have defined the variable “var” taking a value calculated through the division operation performed on two numbers. Any number divided by 0 will throw a “division by zero” error in the output. To avoid that we have been using the except statement to catch the error and save it to the variable “err” as a message. Now, this variable will be printed out with the help of the “print” statement and prevent the program from stopping its execution in between. Let’s just save this code and run it.

try:

Var = 1/0

except Exception as err:

print("Error:", err)

After running this code with the Spyder’s run button, we have got the error as a message as you can see from its console output.

Example 02:

Let’s take a look at another example to print the exception as a message in the Spyder output console. This time, we will be trying to get the “list index out of range” error in the output as a message without terminating our program while executing. So, we have used the try-except statement of python once again. A list of 5 integers is initialized in the “try” statement. After that, we have tried to see the element of the list at the index “10” which is not possible as we have only 5 integer elements in the first 5 indexes. It will cause a list “index out of range” error. This exception will be saved to the “err” variable using the “except” statement and printed out within the console using the print statement of Python.

try:

   list = [4, 8, 12, 16, 20]
   list[10]

except IndexError as err:

   print("Error:", err)

After executing these 5 lines of python code, we have seen that the code is not terminated while the error has been displayed in the output area as a simple message.

Example 03:

Let’s have a look at another example to get an exception message as a display output message in Spyder. We have updated the old code file with the new one shown in the attached picture. So, we have started a “try” statement and performed the multiplication operation between two values. One value is an integer and the other is some variable that is not declared anywhere in the code. Due to this, the interpreter will cause the “NameError” exception upon execution. This may terminate the program and to avoid this, we are using an except statement to catch this error. We have been using the variable “err” to save the exception message in it and display it on the Spyder console using the print statement. Let’s save and run this code.

try:

    var*7

except NameError as err:

    print("Error:", err)

After the debugging, we have got the NameError as a message on the Spyder output area as “name ‘var’ is not defined”. The output is displayed beneath.

Example 04:

Let’s have a look at another error to print its exception message on the output screen of the Python tool. As we all know that you cannot concatenate a string with a number because it will cause a TypeError in python. So, we have tried to concatenate a string “Name” with an integer “15 using the “+” sign in the Spyder3 code area. Due to error, our program would stop working upon running it. We have used the “except” statement here getting the “TypeError” as a message in the “err” variable at run time to avoid this program to stop working. The print statement is quite utilized to display the error message on the output screen of Spyder 3. You can have a look at the updated code.

try:

   'Name' + 15

except TypeError as err:

   print("Error:", err)

After running this code, our program didn’t stop working. Also, it displayed the error “can only concatenate str (not “int”) to str” as a normal message showing that the program is encountered with the TypeError somewhere.

Conclusion:

Finally, we implemented some of the simplest examples to print exception messages as normal text on the output screen of the Spyder 3 Python tool. Within our article, we have covered the examples for different errors i.e. TypeError, NameError, Index out of range Error, and the Division by zero error. These examples can be implemented on any Python tool. So, feel easy to practice anywhere. We hope you will find this article easy to implement.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content

Page was generated in 0.051230907440186