exception - a cleaner way to approach try except in python -


so, let have 3 different calls called something, something1 , something2.

and right now, im calling like

try:       something1    something2 except keyerror e:    print e 

note in above code, if fails, something1 , something2 not executed , on.

the wanted outcome is

try:     except keyerror e:     print e try:     something1 except keyerror e:     print e try:     something2 except keyerror e:     print e 

how can achieve above code without many try except blocks.

edit:

so, answer chose correct worked. of others worked well. chose because simplist , modified little.

here solution based on answer.

runs = [something, something1, something2] func in runs:     try:         func()     except keyerror e:         print e 

you try this, assuming wrap things in functions:

for func in (something, something1, something2):     try:         func()     except keyerror e:         print e 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -