How can I put an action that applied on every exception in python?

46 views Asked by At

I wrote something similar to the following code, and there is duplicated code (process_error) on every exception. But there are duplications.

try:
    do_something()
    ...
except KeyError:
    process_error()
    ...
except ValueError:
    process_error()
    ...
except:
    process_error()
    ...

So I changed the code like this.

try:
    try:
        do_something()
        ...

    except:
        process_error()
        raise
except KeyError:
    ...
except ValueError:
    ...
except:
    ...

I feel like this is very ugly. Is there any better structure than this?

0

There are 0 answers