exceptiongroup icon indicating copy to clipboard operation
exceptiongroup copied to clipboard

API for accessing leaf exception attributes

Open njsmith opened this issue 6 years ago • 0 comments
trafficstars

Sometimes after people catch an exception, they need to access attributes on it (e.g., OSError.errno).

But if you do with catch(OSError, handler): ..., then handler has to be prepared to get an ExceptionGroup, not an OSError. So how can handler access the errno attribute?

I guess the simplest possibility would be a global function accessor like:

def handler(exc):
    for leaf_exc in exceptiongroup.leaf_exceptions(exc):
        print(f"This exception's errno is {leaf_exc.errno}!")

(I don't think it will work to have a method on ExceptionGroup, or making ExceptionGroup iterable, because sometimes when you do with catch(OSError, handler): ... you get a plain OSError, with no ExceptionGroup wrapped around it. This is the same reason split is a function, not a method.)

njsmith avatar Jan 19 '19 08:01 njsmith