As most of you probably know, you can use else with try blocks:

try:
    do_stuff()
except SomeExceptionClass:
   handle_error()
else:
   no_error_occurred()

Here, no_error_occurred will only be called if do_stuff() didn't raise an
exception.

However, the following is invalid syntax:

try:
   do_stuff()
else:
   no_error_occurred()

Now you might say that this isn't needed as you can achieve the same result
with

do_stuff()
no_error_occurred()

However, what if I want to use finally as well:

try:
   do_stuff()
else:
   no_error_occurred()
finally:
   cleanup()

and I need no_error_occurred to be called before cleanup?  For my actual
use case, I've done

try:
   do_stuff()
except Exception:
    raise
else:
   no_error_occurred()
finally:
   cleanup()

This seems very non-Pythonic.  Is there a reason why else without except
has to be invalid syntax?
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/VSXRFQ7OEZJIOCGHWIT225XMGIARFH5J/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to