1

I want to write a decorator, where you could specify any number of exceptions to catch when calling a function (and then how to handle exception output for each exception). Now If I would know the number of exceptions, I could do just:

try:
    fun()
except SomeException1 as e:
    # handle
except SomeException2 as e:
    # handle

But let say I get list of exceptions. exceptions_to_handle = [SomeException1, SomeException2]

Probably could do this, but maybe there is a better way?

try:
    fun()
except Exception as e:
    for exc in exceptions_to_handle:
        if isinstance(e, exc):
            # handle
Andrius
  • 19,658
  • 37
  • 143
  • 243

0 Answers0