Is there a short syntax for catching and processing exception within expressions. E.g if we have some complex expression eint(x)
and another one eext(y)
would be nice to do:
print(
eext(
eint(x)
ecatch (ValueError, AssertionError) as ex1 do f'Exception {ex1} for {x}!'
ecatch IndexError as ex2 do f'Exception {ex2} for {x}!'
)
)
where LEFT ecatch(types, var) RIGHT
returns expression LEFT
if no exception in LEFT, otherwise checks that exception is within types
types, if so then exception value is assigned to var
and RIGHT
expression is evaluated and returned as a result of whole expression. If there is unhandled type of exception then exception is thrown from total expression that may (or may not) be also catched by some other ecatch
expressions on the right.
Of cause such ecatch can be modeled by a simple function accepting lambdas:
def my_ecatch(left, extypes, right):
try:
return left()
except extypes as ex:
return right(ex)
but maybe there is some syntax sugar for doing that already in Python?
Same like lists processing can be also achieved by regular loops, but there exists list comprehension which can look nicer.