python - Detecting Exception Type -
i have set of exceptions defined being raised depending on issue along custom message user.
i develop mechanism of error handling using these custom exceptions , let them bubble higher level logging class , user can provide feedback , tell them fix. end system crapping out, know come bit later.
however, if not 1 of manually created exceptions , system exception (these being raised, wrapped in custom exception), return general error.
when handling exception, way detect if 1 of mine or didn't account for?
i thinking of subclassing exceptions under base 1 custom attribute , checking when letting exception bubble up, seems kind of hacky.
thanks
you can derive exceptions single type. many major python libraries this.
now, see how derived b shared base class?
class a(exception): pass class b(a): pass
let's see if works:
>>> try: >>> raise b >>> except a: >>> print("caught") >>> caught
and doesn't catch unrelated exceptions?
>>> try: >>> [][1] >>> except a: >>> print("caught") >>> indexerror: list index out of range
wonderful.
inheritance friend: subclass single base exception define, , can try catch exception explicitly, log, , rethrow it. if isn't library, error not caught.
Comments
Post a Comment