Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Handle SystemExit exception

Reply
Thread Tools

Handle SystemExit exception

 
 
asit
Guest
Posts: n/a
 
      02-01-2009
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
 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      02-01-2009
En Sun, 01 Feb 2009 07:27:26 -0200, asit <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      02-01-2009
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

 
Reply With Quote
 
asit
Guest
Posts: n/a
 
      02-01-2009
Thanx everyone

This is my fault. Exception was thrown before the main function.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Catching SystemExit in C API code when embedding Python? Stefan Bellon Python 5 08-02-2007 08:30 PM
sys.exit versus raise SystemExit Will McGugan Python 3 01-08-2007 05:48 PM
to Doctest as SystemExit is to Python p.lavarre@ieee.org Python 2 11-10-2006 03:17 PM
doctest, sys.stderr, SystemExit and unused return values Steven Bethard Python 0 07-01-2006 10:20 PM
ServingSocket question regarding the SystemExit exception Jens Thiede Python 0 01-21-2004 06:16 PM



Advertisments