简体   繁体   中英

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:

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

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.

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