什么是处理异常的最佳方式。
class SRException(Exception):
default_exception = True
def somefun1(email):
try:
user = somefun2(email)
except Exception, e:
raise SRException, ("could not find the email", e)
def somefun2(email):
try:
user = User.objects.get(email = email)
except Exception, e:
raise SRException, ("could not find the user objecets",e )
所以当异常发生时,我得到一长串异常
UserProfileException('could not find the user or service objects', UserProfileException('could not find the user', ServicesException('could not find the service', DoesNotExist('Services matching query does not exist.',))))))))
错误和上面的代码示例不一样。但我想我已经表达清楚了。
那么处理异常的最佳方式是什么。 我不应该在每个异常(exception)情况下都提出它吗?每次出现异常时,我都会向技术团队发送邮件。
请您参考如下方法:
您的异常处理程序太宽泛,只捕获您知道可以处理的特定异常。捕获异常只是将其包装在另一个异常中并重新引发它是没有意义的;一个异常对象带有一个 Traceback,它会告诉你代码的运行路径。只需让异常冒泡,然后在可以从异常中恢复的级别捕获它。
