Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Exception to a string in Python 3

does anyone have an idea, why this Python 3.2 code

try:         raise Exception('X') except Exception as e:     print("Error {0}".format(str(e))) 

works without problem (apart of unicode encoding in windows shell :/), but this

try:         raise Exception('X') except Exception as e:     print("Error {0}".format(str(e, encoding = 'utf-8'))) 

throws TypeError: coercing to str: need bytes, bytearray or buffer-like object, Exception found ?

How to convert an Error to a string with custom encoding?

Edit

It does not works either, if there is \u2019 in message:

try:         raise Exception(msg) except Exception as e:     b = bytes(str(e), encoding = 'utf-8')     print("Error {0}".format(str(b, encoding = 'utf-8'))) 

But why cannot str() convert an exception internally to bytes?

like image 487
ts. Avatar asked Aug 16 '11 08:08

ts.


People also ask

How do I get an exception to a text in Python?

Use traceback. print_exc() to print the current exception to standard error, just like it would be printed if it remained uncaught, or traceback. format_exc() to get the same output as a string.

How do I get exception details in Python?

Catching Exceptions in Python In Python, exceptions can be handled using a try statement. The critical operation which can raise an exception is placed inside the try clause. The code that handles the exceptions is written in the except clause.

Can we return value in exception in Python?

You can not do both anyway! Choose what do you want, exit or return. Just guessing, but you probably need to return 0 and have the caller raise SystemExit . There's no way to do both in the same function.

How do you raise an exception in Python?

Python Language Exceptions Re-raising exceptions In this case, simply use the raise statement with no parameters. But this has the drawback of reducing the exception trace to exactly this raise while the raise without argument retains the original exception trace.


2 Answers

In Python 3.x, str(e) should be able to convert any Exception to a string, even if it contains Unicode characters.

So unless your exception actually returns an UTF-8 encoded byte array in its custom __str__() method, str(e, 'utf-8') will not work as expected (it would try to interpret a 16bit Unicode character string in RAM as an UTF-8 encoded byte array ...)

My guess is that your problem isn't str() but the print() (i.e. the step which converts the Python Unicode string into something that gets dumped on your console). See this answer for solutions: Python, Unicode, and the Windows console

like image 183
Aaron Digulla Avatar answered Sep 23 '22 17:09

Aaron Digulla


Try this, it should work.

try:         raise Exception('X') except Exception as e:     print("Error {0}".format(str(e.args[0])).encode("utf-8")) 

Considering you have only a message in your internal tuple.

like image 28
Sebastiano Merlino Avatar answered Sep 21 '22 17:09

Sebastiano Merlino