Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6582931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:20:37+00:00 2026-05-25T16:20:37+00:00

I have a script containing a section similar to this, on Python 2.6: import

  • 0

I have a script containing a section similar to this, on Python 2.6:

import sys

list_id='cow'
prev=[0,'cow']

try:
    if list_id==prev[1]:
        print '{0} is the same as {1}'.format(list_id,prev[1])
        sys.exit(0)
except:
    print 'exception occurred, exiting with error'
    sys.exit(1)

I noticed that though it was printing the ‘is the same’ line, it also logs the exception!

If you remove the try/except block, the interpreter shows no error. If you catch a specific error like ValueError, the except block is not executed.

import sys

list_id='cow'
prev=[0,'cow']

try:
    if list_id==prev[1]:
        print '{0} is the same as {1}'.format(list_id,prev[1])
        sys.exit(0)
except Exception as k:
    print 'exception occurred, exiting with error. Exception is:'
    print k.args
    sys.exit(1)

The except block is not executed, and the process finishes with return code 0. So, the exception is above Exception in the hierarchy?

import sys

list_id='cow'
prev=[0,'cow']

try:
    if list_id==prev[1]:
        print '{0} is the same as {1}'.format(list_id,prev[1])
        sys.exit(0)
except BaseException as k:
    print 'exception occurred, exiting with error. Exception is:'
    print k.args
    sys.exit(1)

produces

cow is the same as cow exception occurred, exiting with error.
Exception is: (0,)

And the process finishes with exit code 1.

Why is this Except block being executed at all?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:20:37+00:00Added an answer on May 25, 2026 at 4:20 pm

    sys.exit() raises SystemExit, which is what you’re seeing.

    As to why it doesn’t inherit from Exception:

    The exception inherits from BaseException instead of StandardError or
    Exception so that it is not accidentally caught by code that catches
    Exception. This allows the exception to properly propagate up and cause the interpreter to exit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a batch file containing a python script using the Output template> %(NAME)s
I have this script which processes lines containing windows file paths. However the script
I have a bash script mystuff containing a line like lynx -dump http://example.com >tmpfile
I have script like this : fullscript.sh if [ ! -f /opt/laks/linux-2.6.33.2.tar.bz2 ] then
I have a script and I'm almost done but I get this error and
I have a small script we're using to read in a CSV file containing
I have a script file containing 1000 lines of data and some of the
I have a script that checks a zipfile containing a number of matching PDF+textfiles.
I have several files containing this line Release: X I want to increment X
I have a greasemonkey script that opens an iframe containing a form from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.