Python: raise another exception in except block to catch later

I want something like this:

trigger = True
    x = my_function()  # This can raise IndexError
    if x is None:
        raise ValueError
except IndexError:
    trigger = False
    raise ValueError
except ValueError:

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?

Read more here:

Content Attribution

This content was originally published by Thijs at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: