codon
codon copied to clipboard
"else" and "finally" blocks are not supported after try construct.
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
finally works; else is not yet supported (should be quite easy to support, I guess).
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.
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?