简体   繁体   English

Python:具有嵌入式错误处理例程的自定义异常?

[英]Python: Custom Exceptions with a embedded error handling routine?

Is it a bad idea to implement my own exceptions with special error handling/logging system embedded? 在嵌入式特殊错误处理/日志记录系统中实现我自己的异常是一个坏主意吗? I wish to concentrate some of my error handling stuff in just one place. 我希望将一些错误处理内容集中在一个地方。

For example: 例如:

errors.py errors.py

class ReallyBadException(Exception):
    def __init__(self, msg, error_handling):
        super(Exception, self).__init__(msg)
        ...
        error_handling.logger.error(msg)
        error_handling.db.do_stuff(msg)
        call_the_police()
        ...

Obviously I know I still will need to handle the exception locally in the main function etc 显然我知道我仍然需要在main函数中本地处理异常等

main.py main.py

from errors import ReallyBadException, NotSoBadException, ...
...  
try: 
    do_something()
except RellyBadException:
    do_another_thing()

Simply put, no this is not a bad idea at all. 简而言之,这不是一个坏主意。

In fact, the way I see it, what you're trying to do is ultimately reason why programming languages have exception handling in the first place. 实际上,按照我的看法,您要尝试做的最终是编程语言首先具有异常处理的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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