Switch find-zero method if convergence fails without try-catch

I’m assuming that you are talking about find_zero from Roots.jl. You could easily modify one line of the code to return the exception instead of throwing it. Then check the return typen instead of the trycatch.

As far as I know, try/catch in Julia is still slow, and the convention is to only throw for serious problems, and to indicate problems via the return type when speed matters.

A “clean” way of doing this would be too add a keyword argument throw_on_failure to find_zero. This argument could have a default value of Val(true), but setting it to Val(false) would cause the exception to be returned instead of thrown. (This causes a type instability in the return value, but that instability is resolved as soon as the calling function verifies that the returned value is indeed a Number.)

2 Likes