[Python-Dev] Re: Syntax for "except"

Michael Chermside mcherm at mcherm.com
Wed Feb 11 07:19:45 EST 2004


Edward Jones writes:
> I would like to propose a class ExceptionList which is an exception 
> containing a list, It can be used in two ways. Suppose
> 
> MyError = ExceptionList( (ValueError, TypeError) )
> 
> "except MyError:"
> handles MyError, ValueError, and TypeError or any exception derived from 
> them. This generalizes the current "except (ValueError, TypeError):".

I don't believe that this *generalizes* using a tuple in an except clause,
I believe it *exactly replicates* that functionality. In other words, I
don't see how

   MyError = ExceptionList( (ValueError, TypeError) )
   try:
       someCode()
   except MyError:
       handleIt()

is any better than this (which works today):

   MyError = (ValueError, TypeError)
   try:
       someCode()
   except MyError:
       handleIt()

So I'd be a strong -1 on providing a new way of doing something which
is already easy to do (hey... it's LESS typing with the current syntax).

Edward Jones continues:
> "raise MyError"
> raises an exception that can be handled by any except clause that 
> handles MyError, ValueError, or TypeError.

Well, that's easy to do in current Python also:

    >>> class CompoundError(TypeError, ValueError):
    ...     pass
    ...
    >>> try:
    ...     raise CompoundError
    ... except ValueError:
    ...     print "it's a value error"
    ...
    it's a value error
    >>> try:
    ...     raise CompoundError
    ... except TypeError:
    ...     print "it's a type error"
    ...
    it's a type error

So again, there's no need for your proposal.

But even if these things WEREN'T already built into Python, I'd STILL
be opposed to adding new syntax to allow them. Catching more than one
exception in the same except clause (the first use) is fairly handy,
but throwing an exception which masquerades as several different
exception types is rather peculiar... I can't think of a realistic way
in which I would want to use this.

-- Michael Chermside




More information about the Python-Dev mailing list