Reputation: 5177
Typically a try/except block can be used to run a bunch of statements until one of them causes an exception.
I want to do the opposite - run a set of statements where EACH of them will likely cause an exception, but one of them will not.
Here's some pseudocode:
try:
c = 1 / 0 # this will cause an exception
c = 1 / (1 - 1) # this will cause an exception
c = int("i am not an integer")
# this will cause an exception
c = 1 # this will not cause an exception
c = 2 # this statement should not be reached
c = None # this would be a final fallback in case everything exceptioned
print c # with this code, c should print "1"
The way I want to use something like this is with data parsing. The user may provide some data which could be in one of several different formats. Attempting to parse the various formats will produce exceptions if the data doesn't match the format. There could be tens of different possible formats. The statements would be listed in order of preference. As soon as one of the parsers succeeds, that's the result I want in the variable.
Wrapping each try inside try/excepts would result in some ugly spaghetti code.
c = None
try:
c = 1 / 0
except:
pass
if (c == None):
try:
c = 1 / (1 - 1)
except:
pass
if (c == None):
try:
c = int("i am not an int")
except:
pass
... and so on
Is there a better way to do this?
Upvotes: 2
Views: 96
Reputation: 4553
I'd say, use lambda functions in an array:
L = [ lambda : 1 / 0,
lambda : 1 / (1 - 1),
lambda : int("i am not an int"),
lambda : 2 ]
for l in L:
try:
x = l()
break
except:
pass
print x
In your current example/request you don't need/use input data to your tests, but eventually you will later on, that's super easy with lambdas.
Upvotes: 3
Reputation: 181027
How about simply making it a function? I'm using your pseudo code, so no better functionality, just more readable;
def doIt():
try:
return 1 / 0
except:
pass
try:
return 1 / (1 - 1)
except:
pass
try:
return int("i am not an int")
except:
pass
c = doIt()
Upvotes: 1