Suggesting the use of StandardError as base of error Exceptions.

Discussion in 'Python' started by Antoon Pardon, Mar 6, 2006.

  1. In a number of cases I have a program that looks like the following.

    for case in all_cases:
    except Exception, ErrInfo:

    The idea is to get as much information as possible when something
    goes wrong but at the same time treat as many cases as possible.

    Then one day things broke. The reason was that in some circumstances
    treat would decide that things were beyond control and called sys.exit
    However sys.exit doesn't return to the O.S. immediately but raises
    SystemExit, which was caugth by the code and the loop continued.

    I then took a look at
    which describes the exception heirarchy as follows:

    +-- SystemExit
    +-- StopIteration
    +-- StandardError
    | +
    | + All kind of error exceptions
    | +
    + All kind of warnings

    and came to the conclusion, that it would be better to write my code
    as follows:

    for case in all_cases:
    except StandardError, ErrInfo:

    Unfortunatly this doesn't work either because a lot of the error
    exceptions in the stdlib (if not all) inherit directly from
    Exception instead of from StandardError. The documentation also
    seems to suggest this use for users exception.

    Now I was wondering if it wouldn't be better that for exception
    that indicate some error condition that these would inherit
    from StandardError and that this would be indicated in the
    documentation and reflected in the stdlib?

    Would it break much code to make this change? My first impression
    would be no, but I could be missing something.
    Antoon Pardon, Mar 6, 2006
    1. Advertisements

  2. There has been a discussion on this just a few days ago, have you read that? There is even a PEP mentioned.

    Diez B. Roggisch, Mar 6, 2006
    1. Advertisements

  3. Antoon Pardon

    Kent Johnson Guest

    It's PEP 352, it's already done. This will be in Python 2.5. It's good
    to see that the time machine is stil working ;)

    Kent Johnson, Mar 6, 2006
  4. You've already been pointed to `PEP 352`_, but just to highlight the
    salient point, I believe what you want to write is::

    except (KeyboardInterrupt, SystemExit):

    ... _PEP 352:

    Steven Bethard, Mar 6, 2006
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.