Python: raise another exception in except block to catch later

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?



Read more here: https://stackoverflow.com/questions/65721291/python-raise-another-exception-in-except-block-to-catch-later

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: