Reputation: 91
I want something like this:
trigger = True
try:
x = my_function() # This can raise IndexError
if x is None:
raise ValueError
except IndexError:
trigger = False
raise ValueError
except ValueError:
do_something()
I want trigger
to be False when IndexError is raised, and do_something()
to happen both if IndexError is raised, or the return value of my_function()
is None. How can I achieve this?
Upvotes: 1
Views: 69
Reputation: 1068
One way to approach this is as follows:
trigger = True
try:
x = my_function() # This can raise IndexError
if x is None:
raise ValueError
except (ValueError, IndexError) as e:
if type(e) is IndexError:
trigger = False
do_something()
Otherwise, you may re-raise an error, but you will need nested try
block, and I assume you don't want that.
Upvotes: 1