codon icon indicating copy to clipboard operation
codon copied to clipboard

"else" and "finally" blocks are not supported after try construct.

Open ghuls opened this issue 2 years ago • 3 comments

else: and finally: blocks are not supported after try construct.

https://www.geeksforgeeks.org/try-except-else-and-finally-in-python/

❯ cat try_except_else_finally.py 
# Python code to illustrate
# working of try() 
def divide(x: int, y: int):
    try:
        # Floor Division : Gives only Fractional
        # Part as Answer
        result = x // y
    except ZeroDivisionError:
        print("Sorry ! You are dividing by zero ")
    else:
        print("Yeah ! Your answer is :", result)
    finally: 
        # this block is always executed  
        # regardless of exception generation. 
        print('This is always executed')  
 
# Look at parameters and note the working of Program
divide(3, 2)
divide(3, 0)
❯ codon run try_except_else_finally.py 
try_except_else_finally.py:10:1: error: unexpected dedent

ghuls avatar Dec 13 '22 12:12 ghuls

finally works; else is not yet supported (should be quite easy to support, I guess).

inumanag avatar Dec 17 '22 18:12 inumanag

I've been hit by this one also.

It seems this construction is not so uncommon. Especially in import sequence.

try:
    import this
except not_there:
    import that
else:
    go on with this

In any case it is part of the Python language. It should be part of Codon.

setop avatar Dec 29 '22 09:12 setop

Hitting this one as well, this would be nice to have :sweat_smile: Anyone have a workaround ? Maybe an ast processor to modify the python code while keeping the same semantic?

laluka avatar Mar 03 '23 20:03 laluka