Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中使用str将异常对象转换为字符串?_Python_Exception Handling - Fatal编程技术网

如何在Python中使用str将异常对象转换为字符串?

如何在Python中使用str将异常对象转换为字符串?,python,exception-handling,Python,Exception Handling,我见过使用str()将异常对象“转换”为错误字符串的代码,如下所示: try: ... except Exception, err: print str(err) err是否包含某个公共“接口”,通过该接口获取其错误字符串? 我希望我的自定义异常类具有相同的功能。我可以考虑的一种方法是为所有异常对象定义一个全局str()方法,然后在其中使用getter返回错误字符串。我应该这样做吗?只需为异常类定义方法。只需为异常类定义方法

我见过使用str()将异常对象“转换”为错误字符串的代码,如下所示:

try:
  ...
except Exception, err:
  print str(err)
err是否包含某个公共“接口”,通过该接口获取其错误字符串? 我希望我的自定义异常类具有相同的功能。我可以考虑的一种方法是为所有异常对象定义一个全局str()方法,然后在其中使用getter返回错误字符串。我应该这样做吗?

只需为异常类定义方法。

只需为异常类定义方法