HomeLinuxHow Do I Convert an Exception to a String in Python

How Do I Convert an Exception to a String in Python


An “exception” is a circumstance that disrupts the conventional stream of directions all through the execution of a program. Python stops executing a program/code when an exception is thrown. Should you’re a Python developer, you’ve most likely run into errors or exceptions in your code. Python offers numerous strategies to deal with these exceptions with the “try-except” block. Nevertheless, generally you could need to convert an exception to a string to get extra details about the error.

On this Python article, we’ll discover the approaches to changing an exception to a Python string.

The best way to Convert an Exception to a Python String?

To transform an exception to a string, apply the next approaches:

Technique 1: Convert an Exceptions to a String in Python Utilizing the “str()” Perform

The best strategy to convert an exception to a string is through the built-in “str()” perform. This perform accepts an object as an argument and retrieves a string illustration of the thing. It returns an error message when the exception object is handed as an argument.

Syntax

 

Right here, “object” is any Python object that may be transformed to a string illustration. The “str()” perform returns a string object.

Instance

The next instance illustrates the mentioned idea:

strive:
    print(1 + ‘3’)
besides Exception as e:
    error_message = str(e)
    print(error_message)
    print(kind(error_message))

 

Within the above code, the “TypeError” exception is caught by the “besides” block. The “besides” block assigns the exception object to the variable “e” and it’s then handed to the “str()” perform as an argument that converts it to a string.

Output

Within the above output, the exception has been transformed right into a string appropriately.

Technique 2: Convert an Exceptions to a String in Python Utilizing “traceback.format_exc()” Perform

The “traceback.format_exc()” perform of the “traceback” module can be used to transform an exception to a string. This perform returns a formatted traceback for the final exception that occurred. Within the traceback, you will see that/get the error message, the situation the place the error occurred, and the perform name stack.

Syntax

traceback.format_exc(restrict=None, chain=True)

 

Within the above syntax:

  • The “restrict (elective)” parameter signifies the variety of stack ranges to point out for every exception within the traceback. The default worth “None” signifies that all ranges are proven.
  • The “chain” parameter is elective and its default worth is “true”. When set to “true”, the perform prints the complete traceback of the exception and all exceptions within the chain. Setting it to “false” prints solely the traceback of the newest exception.

Instance

For instance, let’s take a look at the next code snippet:

import traceback
strive:
    worth = ‘Howdy’
    print(int(worth))
besides Exception as e:
    error_message = traceback.format_exc()
    print(error_message)
    print(kind(error_message))

 

In accordance with the above code, the “try-except” block is used to deal with the exception and print the error message utilizing the “traceback” module. The “traceback.format_exc()” perform retrieves a string that incorporates the stack hint of the exception.

Output

This final result signifies that the exception has been efficiently transformed right into a string.

Technique 3: Convert an Exceptions to a String in Python Utilizing the “repr()” Perform

In Python, the “repr()” perform is used to return a printable illustration of an object in Python. This perform can be utilized to transform an exception right into a string.

Syntax

 

On this syntax, “object” is any Python object whose printable illustration must be returned.

Instance

Take into account the next instance code:

strive:
    worth = ‘Howdy’
    print(int(worth))
besides Exception as e:
    error_message = repr(e)
    print(error_message)
    print(kind(error_message))

 

Within the above code block, the “repr()” perform is used within the “besides” block to catch the confronted exception and convert it right into a string.

Output

In accordance with the above output, the confronted exception has been transformed right into a string.

Conclusion

To transform an exception to a string in Python, apply the “str()” perform, “traceback.format_exc()” perform, or the “repr()” perform. All of those features are used mixed with the “try-except” blocks to catch and convert the confronted exception right into a string. This information offered numerous strategies to transform an exception to a string in Python utilizing quite a few examples.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments