exception attributes

Frederic Giacometti fred at arakane.com
Mon Aug 20 05:13:10 CEST 2001


"Andrew Dalke" <dalke at acm.org> wrote in message
news:9lppoc$8n3$1 at slb2.atl.mindspring.net...
> Frederic Giacometti wrote:
> >String exceptions refer to the exception value used when raising
> >the exception.
>
> Yes.
>
> >But even then, string exceptions must be associated to an exception
class,
> >to which the above rule applies...
>
> Sorry?  I don't understand.  What's the exception class in the following?

I was referring to expressions like
   raise ValueError, "message"
which, roughly, correspond, in C to
   PyErr_String( PyExc_ValueError, "message")
(presently heavily used).

>
> >>> try:
> ...   raise "test2"
> ... except "test1":
> ...   print "Test1"
> ... except "test2":
> ...   print "Test2"
> ... except "test3":
> ...   print "Test3"
> ...
> Test2
> >>>
>
> (NB: this is all old-style Python exceptions.  Use class based
> exceptions for new code.)

Yes, it's for backward compatibility. Why not... Python is 'land of the
free' :))

You'll also note:

>>> try:
...   raise 1
... except 2:
...   print '22'
... except 1:
...   print '2111'
...
Traceback (most recent call last):
  File "<stdin>", line 2, in ?
TypeError: exceptions must be strings, classes, or instances
>>>

All freedom is limited ;))

FG






More information about the Python-list mailing list