catching object

Discussion in 'Python' started by Igor V. Rafienko, Mar 12, 2008.

  1. Hi,


    I was wondering if someone could help me explain this situation:

    h[1] >>> import inspect
    h[1] >>> inspect.getmro(ValueError)
    (<type 'exceptions.ValueError'>, <type 'exceptions.StandardError'>,
    <type 'exceptions.Exception'>, <type 'exceptions.BaseException'>,
    <type 'object'>)
    h[2] >>> try:
    raise ValueError("argh")
    except object:
    print "why not?"

    Traceback (most recent call last):
    File "<stdin>", line 2, in <module>
    ValueError: argh

    The question is, why isn't ValueError caught? It *does* inherit from
    object (albeit indirectly), and my understanding of the wording of
    CPython docs is that this guarantees "compatibility" (between what is
    being raised and what is being caught).

    So, why doesn't object match ValueError (or any other exception for
    that matter). I am aware of "except:", but in my particular situation
    it is eh... unsuitable.

    Any hints/pointers are appreciated.





    ivr
    --
    <+Kaptein-Dah> igorr: for få parenteser
    <+Kaptein-Dah> igorr: parenteser virker som lubrication under iterasjon
    <+Kaptein-Dah> igorr: velkjent
    Igor V. Rafienko, Mar 12, 2008
    #1
    1. Advertising

  2. Igor V. Rafienko

    Stargaming Guest

    On Wed, 12 Mar 2008 01:05:06 +0100, Igor V. Rafienko wrote:

    > Hi,
    >
    >
    > I was wondering if someone could help me explain this situation:
    >
    > h[1] >>> import inspect
    > h[1] >>> inspect.getmro(ValueError)
    > (<type 'exceptions.ValueError'>, <type 'exceptions.StandardError'>,
    > <type 'exceptions.Exception'>, <type 'exceptions.BaseException'>, <type
    > 'object'>)
    > h[2] >>> try:
    > raise ValueError("argh")
    > except object:
    > print "why not?"
    >
    > Traceback (most recent call last):
    > File "<stdin>", line 2, in <module>
    > ValueError: argh
    >
    > The question is, why isn't ValueError caught? It *does* inherit from
    > object (albeit indirectly), and my understanding of the wording of
    > CPython docs is that this guarantees "compatibility" (between what is
    > being raised and what is being caught).


    I think inheritance is meant in reference to the Exception tree here. So,
    the uppermost class is `BaseException`. Python 3 gives this exception
    when trying to handle `object`::

    TypeError: catching classes that do not inherit from BaseException is
    not allowed

    You are right, the wording is quite confusing here but that might be due
    to the crude situation of exceptions: All old-style classes (deprecated
    themselves) can be raised. Strings can still be raised but this is
    deprecated. New-style classes can be raised but only if they inherit (at
    least indirectly) from BaseException. I'd say: feel free to contribute
    patches. :)

    > So, why doesn't object match ValueError (or any other exception for that
    > matter). I am aware of "except:", but in my particular situation it is
    > eh... unsuitable.


    Why so?

    The only possible solution me and my crystal ball could come up with is
    that you're having a lot of old-style exceptions raised from some library
    you cannot modify. You cannot handle any common base class and don't want
    to handle every of the exceptions. If that's the case, just use a bare
    `except` and utilize `sys.exc_info`.

    HTH,
    Stargaming, Mar 12, 2008
    #2
    1. Advertising

  3. [ ]


    [ ... ]

    > I think inheritance is meant in reference to the Exception tree here. So,
    > the uppermost class is `BaseException`. Python 3 gives this exception
    > when trying to handle `object`::
    >
    > TypeError: catching classes that do not inherit from BaseException is
    > not allowed



    I realise that exceptions should inherit from Exception, but the
    question is more "why isn't an instance of ValueError a match for
    object, when ValueError is a subclass of object (at least in 2.5)

    [ ... ]


    > > So, why doesn't object match ValueError (or any other exception for that
    > > matter). I am aware of "except:", but in my particular situation it is
    > > eh... unsuitable.

    >
    > Why so?



    I am writing a function that takes a function and an exception list
    arguments and returns a new function that calls the original while
    trapping the specified arguments. One of the applications is to make a
    wrapper that ignores all of the exceptions. I thought that specifying
    the exception list containing object would suffice to cover that.
    Alas, no joy.

    [ ... ]





    ivr
    --
    <+Kaptein-Dah> igorr: for få parenteser
    <+Kaptein-Dah> igorr: parenteser virker som lubrication under iterasjon
    <+Kaptein-Dah> igorr: velkjent
    Igor V. Rafienko, Mar 14, 2008
    #3
    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. Bob
    Replies:
    5
    Views:
    598
  2. PapaBear
    Replies:
    2
    Views:
    1,209
  3. hg
    Replies:
    1
    Views:
    228
    robert
    Jan 15, 2007
  4. foxx
    Replies:
    4
    Views:
    433
  5. Michael Goerz
    Replies:
    3
    Views:
    404
    Gabriel Genellina
    Feb 4, 2008
Loading...

Share This Page