Quantcast
Channel: cleaner way to handle python exceptions? - Stack Overflow
Viewing all articles
Browse latest Browse all 4

cleaner way to handle python exceptions?

$
0
0

I'm cleaning up some code, and have run into a handful of situations where there are repetitive cleanup actions in a try/except :

try:    ...except KeyError , e :    cleanup_a()    cleanup_b()    cleanup_c()    handle_keyerror()except ValuesError , e :    cleanup_a()    cleanup_b()    cleanup_c()    handle_valueerror()

i'd like to make these a bit more standardized for readability and maintenance. the "cleanup" actions seem to be local to the block , so it's not going to be much cleaner to do the following (though it will standardize it a bit):

def _cleanup_unified():    cleanup_a()    cleanup_b()    cleanup_c()try:    ...except KeyError , e :    _cleanup_unified()    handle_keyerror()except ValuesError , e :    _cleanup_unified()    handle_valueerror()

can anyone suggest alternate ways of approaching this ?


Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>