简体   繁体   中英

How to use str to convert an exception object to string using str in Python?

I have seen code which 'converts' exception objects to the error string using str() like this:

try:
  ...
except Exception, err:
  print str(err)

Does err contain some public 'interface' through which its error string is fetched? I want to have the same functionality for my custom exception class. One way i can think of doing is just define a global str() method for all my exception objects and then inside just return the error string using getter. Is that how I should do it?

只需为您的异常类定义__str__方法即可。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM