[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

catching object

igorr

3/12/2008 12:05:00 AM


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
2 Answers

Stargaming

3/12/2008 1:01:00 PM

0

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,

igorr

3/14/2008 3:21:00 PM

0

[ stargaming@gmail.com ]


[ ... ]

> 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