[SciPy-dev] How to handle exceptional cases in algorithms ? Exception vs Warning
Mon Jun 4 13:23:34 CDT 2007
On 04/06/07, Robert Kern <firstname.lastname@example.org> wrote:
> A problem with exceptions in expensive calculations is that they stop the
> calculation outright. Unless if the calculation is carefully coded, it can't be
> restarted at the point of the exception. This is why we trap hardware floating
> point exceptions and uses NaNs and infs by default in numpy. Parts of the answer
> may be nonsensical, but you get the rest of your data which might be critical to
> debugging the issue.
> I like to reserve exceptions for things that are fatal to the calculation as a
> whole. If the calculation *can't* continue because an assumption gets violated
> in the middle, go ahead and raise an exception. If you can, use a custom
> exception that stores information that can be used to debug the problem.
> Exceptions don't have to just contain a string message.
> However, if you are just running into something unusual, or only technically a
> violation of assumptions (i.e. the results don't really make sense according to
> the algorithm, but the code will still work), issue a custom warning instead.
> Using the warnings module, the user can set things up to raise an exception when
> the warning is issued if he really wants that.
Just a, uh, warning: I found that it was very difficult to make the
warnings module do what it was documented to do in terms of throwing
exceptions and warning the right number of times.
In a recent case I was dealing with, if a matrix failed to be positive
definite (which I noticed because cholesky threw an exception), I fell
back to using the SVD to solve the equation but recorded a "numerical
problems encountered" flag in the object that was running the
computation. I recorded the singular values and the eigenvalues (to
check that some were really negative). I still haven't found the bug,
but at least the code keeps running...
I think my inclination would be, if the algorithm is implemented as
behaviours of an object, use the object to store a description of any
problems that arise.
More information about the Scipy-dev