Re: Is there a way to continue after an exception ?

Discussion in 'Python' started by Krister Svanlund, Feb 20, 2010.

  1. On Sun, Feb 21, 2010 at 12:52 AM, Stef Mientki <> wrote:
    > hello,
    >
    > I would like my program to continue on the next line after an uncaught
    > exception,
    > is that possible ?
    >
    > thanks
    > Stef Mientki
    >


    Yes, you catch the exception and do nothing.
     
    Krister Svanlund, Feb 20, 2010
    #1
    1. Advertising

  2. Krister Svanlund

    Lie Ryan Guest

    > On Sun, Feb 21, 2010 at 12:52 AM, Stef Mientki <> wrote:
    >> hello,
    >>
    >> I would like my program to continue on the next line after an uncaught
    >> exception,
    >> is that possible ?
    >>
    >> thanks
    >> Stef Mientki
    >>


    That reminds me of VB's "On Error Resume Next"
     
    Lie Ryan, Feb 21, 2010
    #2
    1. Advertising

  3. Krister Svanlund

    Lie Ryan Guest

    On 02/21/10 12:02, Stef Mientki wrote:
    > On 21-02-2010 01:21, Lie Ryan wrote:
    >>> On Sun, Feb 21, 2010 at 12:52 AM, Stef Mientki

    <> wrote:
    >>>
    >>>> hello,
    >>>>
    >>>> I would like my program to continue on the next line after an uncaught
    >>>> exception,
    >>>> is that possible ?
    >>>>
    >>>> thanks
    >>>> Stef Mientki
    >>>>
    >>>>

    >> That reminds me of VB's "On Error Resume Next"
    >>

    > I think that's what I'm after ...


    First, read this:
    http://www.developerfusion.com/code/4325/on-error-resume-next-considered-harmful/

    > I already redirected sys.excepthook to my own function,
    > but now I need a way to get to continue the code on the next line.
    > Is that possible ?


    No, not in python. You can (ab)use generators' yield to resume
    execution, but not in the general case:

    def on_error_resume_next(func):
    def _func(*args, **kwargs):
    gen = func(*args, **kwargs)
    resp = next(gen)
    while isinstance(resp, Exception):
    print 'an error happened, ignoring...'
    resp = next(gen)
    return resp
    return _func

    @on_error_resume_next
    def add_ten_error_if_zero(args):
    if args == 0:
    # raise Exception()
    yield Exception()
    # return args + 10
    yield args + 10

    print add_ten_error_if_zero(0)
    print add_ten_error_if_zero(10)




    A slightly better approach is to retry calling the function again, but
    as you can see, it's not appropriate for certain cases:

    def retry_on_error(func):
    def _func(*args, **kwargs):
    while True:
    try:
    ret = func(*args, **kwargs)
    except Exception:
    print 'An error happened, retrying...'
    else:
    return ret
    return _func

    @retry_on_error
    def add_ten_error_if_zero(args):
    if args == 0:
    raise Exception()
    return args + 10

    print add_ten_error_if_zero(0)
    print add_ten_error_if_zero(10)



    A much better approach is to use callbacks, the callbacks determines
    whether to raise an exception or continue execution:

    def handler(e):
    if datetime.datetime.now() >= datetime.datetime(2012, 12, 21):
    raise Exception('The world has ended')
    # else: ignore, it's fine

    def add_ten_error_if_zero(args, handler):
    if args == 0:
    handler(args)
    return args + 10

    print add_ten_error_if_zero(0, handler)
    print add_ten_error_if_zero(10, handler)
    print add_ten_error_if_zero(0, lambda e: None) # always succeeds



    Ignoring arbitrary error is against the The Zen of Python "Errors should
    never pass silently."; not that it is ever a good idea to ignore
    arbitrary error, when an exception happens often the function is in an
    indeterminate state, and continuing blindly could easily cause havocs.
     
    Lie Ryan, Feb 21, 2010
    #3
  4. Krister Svanlund

    Guest

    On Feb 20, 2010, at 9:17 PM, Lie Ryan wrote:

    > On 02/21/10 12:02, Stef Mientki wrote:
    >> On 21-02-2010 01:21, Lie Ryan wrote:
    >>>> On Sun, Feb 21, 2010 at 12:52 AM, Stef Mientki

    > <> wrote:
    >>>>
    >>>>> hello,
    >>>>>
    >>>>> I would like my program to continue on the next line after an uncaught
    >>>>> exception,
    >>>>> is that possible ?
    >>>>>
    >>>>> thanks
    >>>>> Stef Mientki
    >>>>>
    >>>>>
    >>> That reminds me of VB's "On Error Resume Next"
    >>>

    >> I think that's what I'm after ...

    >
    > First, read this:
    > http://www.developerfusion.com/code/4325/on-error-resume-next-considered-harmful/


    The link goes to an "Oh dear. Gremlins at work!" page.

    They're probably using On Error Resume Next in their VBScript code and this is the "last resort" page ;-).

    S
     
    , Feb 21, 2010
    #4
  5. Krister Svanlund

    Ryan Kelly Guest

    On Sun, 2010-02-21 at 13:17 +1100, Lie Ryan wrote:
    > On 02/21/10 12:02, Stef Mientki wrote:
    > > On 21-02-2010 01:21, Lie Ryan wrote:
    > >>> On Sun, Feb 21, 2010 at 12:52 AM, Stef Mientki

    > <> wrote:
    > >>>
    > >>>> hello,
    > >>>>
    > >>>> I would like my program to continue on the next line after an uncaught
    > >>>> exception,
    > >>>> is that possible ?
    > >>>>
    > >>>> thanks
    > >>>> Stef Mientki
    > >>>>
    > >>>>
    > >> That reminds me of VB's "On Error Resume Next"
    > >>

    > > I think that's what I'm after ...

    >
    > A much better approach is to use callbacks, the callbacks determines
    > whether to raise an exception or continue execution:
    >
    > def handler(e):
    > if datetime.datetime.now() >= datetime.datetime(2012, 12, 21):
    > raise Exception('The world has ended')
    > # else: ignore, it's fine
    >
    > def add_ten_error_if_zero(args, handler):
    > if args == 0:
    > handler(args)
    > return args + 10
    >
    > print add_ten_error_if_zero(0, handler)
    > print add_ten_error_if_zero(10, handler)
    > print add_ten_error_if_zero(0, lambda e: None) # always succeeds



    Or if you don't like having to explicitly manage callbacks, you can try
    the "withrestart" module:

    http://pypi.python.org/pypi/withrestart/

    It tries to pinch some of the good ideas from Common Lisp's
    error-handling system.

    from withrestart import *

    def add_ten_error_if_zero(n):
    # This gives calling code the option to ignore
    # the error, or raise a different one.
    with restarts(skip,raise_error):
    if n == 0:
    raise ValueError
    return n + 10

    # This will raise ValueError
    print add_ten_error_if_zero(0)

    # This will print 10
    with Handler(ValueError,"skip"):
    print add_ten_error_if_zero(0)

    # This will exit the python interpreter
    with Handler(ValueError,"raise_error",SystemExit):
    print add_ten_error_if_zero(0)



    Cheers,

    Ryan


    --
    Ryan Kelly
    http://www.rfk.id.au | This message is digitally signed. Please visit
    | http://www.rfk.id.au/ramblings/gpg/ for details


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.9 (GNU/Linux)

    iEYEABECAAYFAkuAn6UACgkQfI5S64uP50qeIgCfQPRe+cP/8M3Nb/mwOn9dVmx8
    I3AAoKY/KE6nmVhF3Ib1E9eBFFKF1oul
    =o+aQ
    -----END PGP SIGNATURE-----
     
    Ryan Kelly, Feb 21, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Beemer Biker

    [newbie] is there an OnServerError continue?

    Beemer Biker, Nov 3, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    282
    =?Utf-8?B?TmV2ZXJseW4=?=
    Nov 6, 2006
  2. xz
    Replies:
    13
    Views:
    561
  3. Steve
    Replies:
    2
    Views:
    1,446
    Leonard Milcin
    May 1, 2008
  4. aidy
    Replies:
    2
    Views:
    203
    Pit Capitain
    Sep 22, 2006
  5. mirth
    Replies:
    11
    Views:
    534
    mirth
    Feb 20, 2008
Loading...

Share This Page