This is *exactly* what Python does right now!

class ToFailError(Exception): pass

try:
    do_some_stuff()
    if (some_error):
        raise ToFailError
    do_other_stuff()
    try:
       thing_that_might_fail()
    except SomeOtherError:
       handle_it()

except ToFailError:
    here_we_are()


On Wed, Mar 5, 2014 at 7:54 PM, Sturla Molden <sturla.molden@gmail.com> wrote:
"goto fail" is a well-known error handling mechanism in open source software, widely reputed for its robusteness:

http://opensource.apple.com/source/Security/Security-55471/libsecurity_ssl/lib/sslKeyExchange.c

https://www.gitorious.org/gnutls/gnutls/source/6aa26f78150ccbdf0aec1878a41c17c41d358a3b:lib/x509/verify.c

I believe Python needs to add support for this superior paradigm.

It would involve a new keyword "fail" and some means of goto'ing to it. I suggest "raise to fail":

if (some_error):
   raise to fail

fail:
   <error handling code>

Unless there are many objections, this fantastic idea might be submitted in a (short) PEP somewhere around the beginning of next month.

There is some obvious overlap with the rejected "goto PEP" (PEP 3163) and the Python 2.3 goto module. However, the superiority of goto fail as error generation and error handling paradigm has since then been thoroughly proven.

http://legacy.python.org/dev/peps/pep-3136/
http://entrian.com/goto/download.html


Regards,
Sturla Molden







_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/



--
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.