Handle SystemExit exception

Discussion in 'Python' started by asit, Feb 1, 2009.

  1. asit

    asit Guest

    My program contains some sys.exit(1) and when its executed an
    exception known as SystemExit is thrown.

    I tried to handle bu using following code snippet

    if __name__ == "__main__":
    try:
    main() #main contains sys.exit(1)
    except KeyboardInterrupt:
    print "aborted by user"
    except SystemExit:
    pass

    But it does not work.

    Can anyone help me out ???

    Though KeyboradInterrupt is handled, but why SystemExit is not
    handles ????

    Regards
    Asit Dhal
    asit, Feb 1, 2009
    #1
    1. Advertising

  2. En Sun, 01 Feb 2009 07:27:26 -0200, asit <> escribió:

    > My program contains some sys.exit(1) and when its executed an
    > exception known as SystemExit is thrown.


    This is normal behaviour - but you should not "see" the SystemExit. Do you
    mean that a stack trace appears in the console?

    > I tried to handle bu using following code snippet
    >
    > if __name__ == "__main__":
    > try:
    > main() #main contains sys.exit(1)
    > except KeyboardInterrupt:
    > print "aborted by user"
    > except SystemExit:
    > pass
    >
    > But it does not work.


    I've tested adding this:

    def main():
    sys.exit(1)

    and it exited normally, even after removing the SystemExit clause.

    > Can anyone help me out ???
    >
    > Though KeyboradInterrupt is handled, but why SystemExit is not
    > handles ????


    Perhaps there is another try/except somewhere else that handles SystemExit?
    Look for bare except clauses, like

    try:
    except: ...

    They should *not* be used. With Python 2.5 and above, theh "catch-all"
    should be written as:

    try:
    except Exception: ...

    If you are using 2.4 or earlier, should be:

    try:
    except (SystemExit, KeyboardInterrupt): raise
    except: ...

    --
    Gabriel Genellina
    Gabriel Genellina, Feb 1, 2009
    #2
    1. Advertising

  3. asit wrote:

    > My program contains some sys.exit(1) and when its executed an
    > exception known as SystemExit is thrown.
    >
    > I tried to handle bu using following code snippet
    >
    > if __name__ == "__main__":
    > try:
    > main() #main contains sys.exit(1)
    > except KeyboardInterrupt:
    > print "aborted by user"
    > except SystemExit:
    > pass
    >
    > But it does not work.


    It works for me:

    >>> try:

    .... sys.exit()
    .... except SystemExit:
    .... pass
    ....
    >>>



    What does it do for you? Crash? Exit? print garbage to the screen?


    I'm going to guess what you are doing. Do you have code like this?

    try:
    main() #main contains sys.exit(1)
    except KeyboardInterrupt:
    print "aborted by user"
    except SomeOtherException, SystemExit:
    pass

    This will mask SystemExit and stop it from being caught. It should be
    written like this:

    except (SomeOtherException, SystemExit):
    pass



    Or possibly something like this?

    try:
    main() #main contains sys.exit(1)
    except KeyboardInterrupt:
    print "aborted by user"
    except Exception:
    do_something()
    raise
    except SystemExit:
    pass

    The Exception clause will catch SystemExit before the SystemExit clause will
    see it. That should be written like this:


    except SystemExit:
    pass
    except Exception:
    do_something()
    raise




    --
    Steven
    Steven D'Aprano, Feb 1, 2009
    #3
  4. asit

    asit Guest

    Thanx everyone

    This is my fault. Exception was thrown before the main function.
    asit, Feb 1, 2009
    #4
    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. Jens Thiede
    Replies:
    0
    Views:
    257
    Jens Thiede
    Jan 21, 2004
  2. Steven Bethard
    Replies:
    0
    Views:
    384
    Steven Bethard
    Jul 1, 2006
  3. Replies:
    2
    Views:
    307
  4. Will McGugan

    sys.exit versus raise SystemExit

    Will McGugan, Jan 8, 2007, in forum: Python
    Replies:
    3
    Views:
    370
    Duncan Booth
    Jan 8, 2007
  5. Stefan Bellon
    Replies:
    5
    Views:
    444
    Stefan Bellon
    Aug 2, 2007
Loading...

Share This Page