HomeLinuxPython Print Exception Message

Python Print Exception Message


As a Python developer, you need to have encountered “errors” or “exceptions” in your Python code. These errors could be “syntax errors”, “logical errors”, or “runtime errors”. When an error takes place in Python, the interpreter raises/throws an “exception”, which could be caught/captured and dealt with via exception-handling methods. “Printing exception” messages in Python is an easy course of. When an exception is raised, the interpreter prints an error message together with the kind of exception.

These articles present the next strategies to print/show exception messages in Python:

Technique 1: Print Exception Messages in Python Utilizing the “print()” Operate

The “print()” operate is used to print/show the exception message. This technique makes use of the “try-except” block to catch the exception, and the “print()” operate to show the error message.

Instance

The under code is used to print the exception message:

strive:

print(‘Python’+ 32)

besides Exception as err:

print(“An error occurred:”, err)

Within the above code, catch any exception that happens within the “strive” block and print the error message together with the kind of exception utilizing the “print()” operate.

Output

Based mostly on the above output, the exception message has been displayed efficiently.

Technique 2: Print Exception Messages in Python Utilizing the “logging” Module

In Python, the “logging” module will also be used to print exception messages. This module gives a solution to log messages and errors to a file or on the console.

Instance

To print the exception message, use the next code:

import logging

strive:

x = ‘Python’

print(int(x))

besides Exception as e:

logging.error(“An error occurred: %s”, e)

Within the above code snippet, the “logging” module is imported at first and is used to catch any exception that happens within the “strive” block. The “logging.error()” technique is used within the “besides” block to log the error message together with the kind of exception.

Output

Exception message efficiently displayed within the above snippet.

Technique 3: Print Exception Messages in Python Utilizing the “traceback” Module

The “traceback” module presents a solution to print detailed details about an exception, together with the traceback and the road quantity the place the exception occurred.

Instance

The below-given code is used to print the exception message on the console:

import traceback

strive:

print(math.exp(4))

besides Exception as e:

traceback.print_exc()

In accordance with the above code, the “traceback” module is imported. After that, the “traceback.print_exc()” operate is utilized within the “besides” block to catch/seize and print any exception message that happens within the “strive” block.

Output

On this output, the exception message is logged appropriately.

Conclusion

The “print()” operate, the “logging” module, or the “traceback” module can be utilized together with the “try-except” block to print exception messages in Python. These strategies present an environment friendly solution to print the exception and repair the error based mostly on the exception message. This Python article lined an in depth information on tips on how to print an exception message utilizing varied methods.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments