Python

How Do I Convert an Exception to a String in Python?

Exceptions i.e., errors are very common in programming while execution. These exceptions can be caused due to some logical errors, some syntax issues, or some system or software configuration issues. The exceptions can cause your program to quit immediately. To avoid this quick stop of execution, we have been known with the very well-known try-catch statement. There is one other method to handle such exceptions to cause the stop of a program.

So, we will make use of conversion within the program to convert an exception to string in Python. Make sure you have python3 configuration in your Linux system. Let’s have a fresh start with opening the console application as we have to work on the terminal using the Ctrl+Alt+T.

Example 1:

So, we have started with the creation of a new Python file in the shell. This has been done using the “touch” query in the shell. After that, we have been opening the file with a GNU Nano editor to create some Python code within it. Both the commands are stated in the image.

$ touch convert.py

$ nano convert.py

After opening the file in an editor, we have used the python3 support at the top line to make it executable. We have added a simple code first to see how the exception occurs in the shell. So, we have initialized a list “list” with a value of 12. The list has been incremented with 5 using the incremented operator as “+=” at the next consecutive line.

#!/usr/bin/python3

list = [12]

list += 5

Used the python3 keyword of a Python package to run our code file i.e., “convert.py”. In return, we have got an exception stating the “TypeError” exception. It states that the “int” integer is not iterable in the case of lists. It must be used for some integer type variable. The output of the script can be seen in the affixed image.

$ python3 convert.py

This is how the exception occurs and stops the execution of any program code within the shell terminal of the Ubuntu 20.04 system. Let’s resolve this issue by converting an exception to a string, making it display on the shell as a normal string, and preventing it from stopping the execution. So, after opening the file, we have added python-support. The try-except statement will be utilized for this purpose so far.

Within the try statement, we will be adding our Python code to be executed and will cause an error i.e., initialization of list and increment it. The except statement has been used here to get the exception error in variable “e”. The exception will be converted into a string i.e., str, and saved into the variable “string”. The string variable will be printed out in the shell at the end. Save the updated Python code with the Ctrl+S shortcut.

#!/usr/bin/python3

Try:

   list = [12]

   list += 5

except Exception as e:

   string = str(e)

   print(“The error is: ”, string)

We have got the exception as a string in the shell and the program does not stop executing. The output of the script can be seen in the affixed image.

$ python3 convert.py

Example 2:

Let’s have another simple example to get the exception into a string to prevent the program from stopping. We have launched the same file in the Nano editor and added python3 support. The try statement contains the initialization of a list while concatenating it with an integer value. The except statement is getting the error, converting it to string, saving to a variable, and printing it.

#!/usr/bin/python3

Try:

   list = [12] + 1

except Exception as e:

   string = str(e)

   print(“Error: ”, string)

We have got the “concatenation” error as a resultant string on the shell instead of an error. The output of the script can be seen in the affixed image.

$ pyhton3 convert.py

Conclusion

This article contains the implementation of converting an exception to a string and displaying it as normal text on the shell. We have utilized two simple and easy Python examples to illustrate this concept to our users. We eagerly hope and looking forward to your kind feedback.

About the author

Kalsoom Bibi

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