Python - 发生异常时发送电子邮件? [英] Python -- Send Email When Exception Is Raised?

查看:303
本文介绍了Python - 发生异常时发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



方法1() )



Method2()



...........



...........



MethodN()



所有方法 - 执行不同任务时 - 具有相同的方案:

 做某事
做别的事
有什么问题吗?
提出异常






我想每当类中的任何地方出现异常时,都可以收到电子邮件。



有没有一些简单的方法将这个逻辑组合到类中,而不是调用 SendEmail()在每个之前提交异常语句?什么是正确的,pythonic的方式来处理这种情况? canh一个泛化的异常处理程序是解决方案?我会很高兴你可能有任何想法。

解决方案

持续一秒。我重读了你的问题,突然觉得这个问题的答案很简单。如果可以这样做:

  if problem_test():
SendEmail()
raise异常

那么你为什么不定义一个自定义的 raise_email 方法?

  def raise_email(self,e):
SendEmail()
raise e


I have a python class with many methods():

Method1()

Method2()

...........

...........

MethodN()

All methods -- while performing different tasks -- have the same scheme:

do something
do something else
has anything gone wrong?
    raise an exception


I want to be able to get an email whenever an exception is raised anywhere in the class.

Is there some easy way to combine this logic into the class, rather than calling SendEmail() before every raise Exception statement? what is the right, pythonic way to deal with such a case? canh a 'generalized' Exception handler be the solution? I'd be glad for any ideas you may have.

解决方案

Hold on a second. I reread your question, and suddenly feel that the answer to this problem is very simple. If the alternative is this:

if problem_test():
    SendEmail()
    raise Exception

Then why don't you just define a custom raise_email method?

def raise_email(self, e):
    SendEmail()
    raise e

这篇关于Python - 发生异常时发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆