jeudi 14 mars 2019

Try, except clause with if, else

Let's imagine this code:

    try:
        if condition1 and condition2: # some_exception may happen here
            function1()
        elif condition3 and condition4: # some_exception may happen here
            function2()
        else:
            big
            block
            of
            instructions
    except some_exception:
        big
        block
        of
        instructions

As you can see I repeat big block of instructions (both are same). Is there a way to avoid repetition, but something different than putting the code in a function?

Some kind of different logic or using finally or else to try? I just can't figure it out.

Thanks in advance for helping me!

Aucun commentaire:

Enregistrer un commentaire