What Exceptions are there? (was: "a better input")

Andrew Dalke dalke at dalkescientific.com
Fri May 10 11:02:57 EDT 2002


Louis M. Pecora:
>Ok, you knew there was an exception of that type (ValueError), but is
>there a way to get a list of exceptions in a Python module or in the
>main Python core?  There are probably zillions.  No?  How does one know
>what exceptions exist, i.e. what the programmer was thinking at the
>time?

I believe you are asking for some way to find which exceptions can
be raised by a function.  No, there isn't a way in Python.  Consider

def add(a, b):
   return a+b

class MyA:
  def __add__(self, other):
    raise MemoryError

add(MyA(), 8)

An analysis of 'add' alone wouldn't suggest that it could throw a
MemoryError, because it needs to know about 'a' and 'b' before it
knows that.

Languages like Java have an explicit, required declaration of the
(non-standard exceptions) that are thrown.  As I recall (it's been
years since I looked at Java), it's even a compilation error if there's
a possibility that an undeclared exception is used.

Python has no support for this.  But I also recall not liking Java's
approach because it makes callbacks harder to work with.  Eg, suppose
you have a numeric integrator for the function 'f', in this case
between 0 and 100

  area = integrate(f, 0, 100)

Now I want to replace 'f' with an RPC call that does the calculations
on a much faster machine (or one that has the appropriate software
license.)

  area = integrate(RPC(service = "calcf", machine = "remote.hostname"),
                   0, 100)

The RPC call can fail because of network problems.  But those
network exceptions aren't declared in 'integrate's prototype, if there
were one.  So I think either the exception is mapped to a generic
exception in Java (akin to "unsupported exception thrown") or you
need to write an adapter to filter out the new exceptions.

Never did find anyone who could tell me the proper Java way to solve
this problem...

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list