All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Python Get Exception Message

Last Updated : Mar 11, 2024

Python Get Exception Message

In this article we will show you the solution of python get exception message, Python has a wide range of functionality for handling exceptions. An exception event stops a programme from working and, if it is not handled, ends it immediately.

The ValueError, TypeError, and IndexError are the most well-known instances.

Your programme will end instantly if there is an exception. To prevent this, you can place a try/except block around the code where you anticipate that a certain exception would occur to catch the exception.

This is how to recognise and print a specific exception:

When an exception occurs in a piece of code, it should be caught and printed by enclosing it in an indented try block and then using the command "except Exception as e" to preserve the exception's message in the string variable e.

The error message can now be printed with "print(e)" or used for additional processing.

We will now discuss the idea of how to get an exception message in python with an example.

Step By Step Guide On Python Get Exception Message :-

Code 1

import logging
logger=logging.getLogger()
num1=int(input("Enter no 1:"))
num2=int(input("Enter no 2:"))
try:
  print("Result: ", num1/num2)
except Exception as e:
  logger.exception("Exception Occured while code Execution: "+ str(e))

Code 2

# Creating an empty list
new_list =[]
n = int(input("TC Example Of Enter number of elements : "))
for i in range(n):
  try:
    item = int(input())
    # Add the item in the list
    new_list.append(item)
  except:
    print("Invalid Input!")
    print("Next entry.")
print("The list entered by user is: ", new_list)
  1. For logging failures and exceptions, the "logging" module is imported.
  2. The command "logger=logging.getLogger()" returns the root logger.
  3. "num1" and "num2" are two integers that the user enters.
  4. Any exceptions that arise when performing the division operation "num1/num2" are handled in a "try-except" block.
  5. The division operation is carried out and the outcome is printed inside the "try" block.
  6. The "except" block is carried out if an exception occurs.
  7. The exception object is converted to a string and the exception message is written to the log using the "logger.exception()" method.
  8. The console is printed with the logged message.
  9. Programme execution is completed.

Conclusion :-

As a result, we have successfully learned how to get an exception message in python with an example.

When an exception occurs in a piece of code, it should be caught and printed by enclosing it in an indented try block and then using the command "except Exception as e" to preserve the exception's message in the string variable e.

The error message can now be printed with "print(e)" or used for additional processing.

I hope this article on python get exception message helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪