How do I know all thrown exceptions of a function?

Jeff Petkau jpet at eskimo.com
Wed Jan 24 03:22:52 EST 2001


Andrew Dalke <dalke at acm.org> wrote in message
news:94imre$7dd$1 at slb6.atl.mindspring.net...
> def integrate(func, x_min, x_max):
>   ...
>   will compute func(x) for several values of x between [x_min, x_max]
>   ...
>
> What exceptions does "integrate" throw, given that "func" can
> throw anything?  As I understand the Java way, you must declare
> all of the exceptions thrown by "func", which most people will
> take to mean it only throws a math error (divide by zero, overflow,
> etc.).  Once the "func" exceptions are known, the "integrate"
> ones are also known.
>...
> I actually don't know.  As far as I can tell, the only way to
> do it is let integrate raise a specific "CannotIntegrate"
> exception, then implement a wrapper, like for MyCache, which
> converts any non-numeric exception in the CannotIntegrate
> exception, as in
>
> To me, this is ugly and error prone.  (Is this really what
> you need to do for Java - the Feb. Dr. Dobb's comparison
> between Java and C# suggests so.)  Thus, I would rather write
> my code to be exception safe, in that it gracefully accepts
> any exception, then to declare every exception type.

Since all exceptions in Java are derived from java.lang.Throwable,
you could just declare integrate() as throwing Throwable. So
you're effectively declaring, "this function can throw any damn
fool thing, you deal with it."

I think it's a good solution, although parameterized types
would be better. (So you could say "this function throws
whatever func throws.")

--Jeff






More information about the Python-list mailing list