LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-02-2022, 04:57 PM   #1
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Rep: Reputation: 51
Exclamation python: sys.exit inside a try block


Did you know that if i call sys.exit(0) inside a try block, the program will cause an exception? Do you consider this a bug?

Code:
"""
bla bla bla
"""
import sys

try:
    print( "oi" )
    sys.exit(0)
except:
    print( "erro" )
    print( sys.exc_info() )
    sys.exit(1)

print( 'fim?' )

# vim: fileencoding=utf-8: expandtab: shiftwidth=4: tabstop=8: softtabstop=4
The output of this code is:

Code:
$ p3 min3.py 
oi
erro
(<class 'SystemExit'>, SystemExit(0), <traceback object at 0x7f1160bdd700>)

$ # and exit code of was 1, not 0!
 
Old 07-02-2022, 05:01 PM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,610

Rep: Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553

It's not a bug, it's the documented behaviour of sys.exit()

Quote:
Originally Posted by https://docs.python.org/3/library/sys.html#sys.exit
Raise a SystemExit exception, signaling an intention to exit the interpreter.
...
Since exit() ultimately “only” raises an exception, it will only exit the process when called from the main thread, and the exception is not intercepted. Cleanup actions specified by finally clauses of try statements are honored, and it is possible to intercept the exit attempt at an outer level.
 
1 members found this post helpful.
Old 07-02-2022, 05:48 PM   #3
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
Thumbs up

Okey. This is a surprise to me. Thank you for explaining and pointing the documentation, boughtonp. (:
 
  


Reply

Tags
python, sys



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] want to try ubuntu 10.10 installation from pendrive inside 'try ubuntu' hobomystical Linux - Newbie 6 02-12-2011 10:05 AM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM
[SOLVED] Need to manually re-install sys-lib/com_err and sys-lib/ss matsvw Gentoo 4 08-02-2009 11:17 AM
How to exit and save (or just to exit) inside a "make config" running command? scrat75 Linux - Kernel 6 09-05-2008 04:02 PM
Is a Linux sys w/o any passwd's as secure as a microsoft windows sys w/no passwd? t3gah Linux - Security 4 04-22-2005 08:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration