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

**URL:** <https://discourse.julialang.org/t/switch-find-zero-method-if-convergence-fails-without-try-catch/50482>\
**Category:** General Usage\
**Tags:** performance, exception, roots\
**Created:** [November 20, 2020, 10:02am UTC](https://discourse.julialang.org/t/switch-find-zero-method-if-convergence-fails-without-try-catch/50482 "2020-11-20T10:02:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jacob\_Stevens](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob_stevens/32/19496_2.png) [@Jacob\_Stevens](https://discourse.julialang.org/u/Jacob_Stevens)\
**Post date:** [November 20, 2020, 10:02am UTC](https://discourse.julialang.org/t/switch-find-zero-method-if-convergence-fails-without-try-catch/50482/1 "2020-11-20T10:02:57Z")

</div>

I have an inner loop which solves a simple equation using find\_zero(), currently with the default Order0() method at a plausible starting value. 99.99% of the time, this works smoothly with no issues. However, occasionally this does fail to converge, and when this happens I’d like to switch method to a bisection approach with a very large interval. The obvious method would seem to be:

```julia
try
    result = find_zero(f, ini)
catch
    result = find_zero(f, [0, Big_Number])
end

```

However, my concern is that try/catch apparently slows down code significantly (e.g. [here](https://discourse.julialang.org/t/are-exceptions-in-julia-zero-cost/38405)) even when try completes successfully. This would be lethal to my code, since this inner loop is being run an awful lot. So my questions are:

- Is there still a significant performance penalty to try/catch as of 1.5.3?
- Is there another way to achieve “try algorithm A, if it fails try B” without compromising performance?

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [November 20, 2020, 10:51am UTC](https://discourse.julialang.org/t/switch-find-zero-method-if-convergence-fails-without-try-catch/50482/2 "2020-11-20T10:51:17Z")

</div>

I’m assuming that you are talking about `find_zero` from Roots.jl. You could easily modify [one line](https://github.com/JuliaMath/Roots.jl/blob/0a2c18a55fd0bac3e76c591514c14bcb469a260f/src/find_zero.jl#L670) of the code to `return` the exception instead of `throw`ing it. Then check the return typen instead of the `try` … `catch`.

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`.)

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [November 20, 2020, 11:18pm UTC](https://discourse.julialang.org/t/switch-find-zero-method-if-convergence-fails-without-try-catch/50482/3 "2020-11-20T23:18:30Z")

</div>

I made an issue for this with a PR to do this a slightly different way.

[https://github.com/JuliaMath/Roots.jl/issues/196](https://github.com/JuliaMath/Roots.jl/issues/196)
