Re: [Python-ideas] Let try-except check the exception instance

In one of my own packages, I use the following to catch dynamic error messages: def handle_error(exc): # do stuff with exc with catch('error_code', handle_error): # do stuff that can raise an error The definition of "catch" is as follows, assuming the root exception actually raised is called "CodedError" whose "code" attribute is the error code: from contextlib import contextmanager @contextmanager def catch(code=None, caught=None, always=None): try: yield except CodedError as exc: if isinstance(code, str): is_caught = exc.code == code else: is_caught = exc.code in code #assume it's a container if (code is not None) and not is_caught: raise if caught is not None: caught(exc) finally: if always is not None: always() Perhaps you could make use of this? Suggesting, Ken Hilton;
participants (1)
-
Ken Hilton