# Is there any speed improvement from wrapping invokelatest in a try-catch?

**URL:** <https://discourse.julialang.org/t/is-there-any-speed-improvement-from-wrapping-invokelatest-in-a-try-catch/7958>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 24, 2017, 1:35am UTC](https://discourse.julialang.org/t/is-there-any-speed-improvement-from-wrapping-invokelatest-in-a-try-catch/7958 "2017-12-24T01:35:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [December 24, 2017, 1:35am UTC](https://discourse.julialang.org/t/is-there-any-speed-improvement-from-wrapping-invokelatest-in-a-try-catch/7958/1 "2017-12-24T01:35:02Z")

</div>

For example, let’s say you dynamically make a module `Foo`:

```julia
module Foo
  nothing_here_yet = true
end

```

Is it worthwhile to put an invokelatest try-catch to avoid world\_age bugs, while still keeping most of the speed?

```julia
try
  Foo.eval(cur_import)
catch
  Base.invokelatest(
    Foo.eval,
    cur_import
  )
end

```

// one related questions might be: how much does a try-catch loop hurt you (performance-wise)?

_edit: more specifically, if the catch part never gets touched_

---

<div class="post-metadata">

**Author:** ![vchuravy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vchuravy/32/8_2.png) [@vchuravy](https://discourse.julialang.org/u/vchuravy)\
**Post date:** [December 24, 2017, 9:40am UTC](https://discourse.julialang.org/t/is-there-any-speed-improvement-from-wrapping-invokelatest-in-a-try-catch/7958/2 "2017-12-24T09:40:52Z")

</div>

I don’t know, but a `try catch` can be quite costly. So it might be best for you to actually test the difference.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 24, 2017, 9:51am UTC](https://discourse.julialang.org/t/is-there-any-speed-improvement-from-wrapping-invokelatest-in-a-try-catch/7958/3 "2017-12-24T09:51:02Z")

</div>

There was a [recent discussion](https://discourse.julialang.org/t/type-stable-matrix-factorization-corner-case-with-forwarddiff/7791) where I found `try ... catch` to be a reasonable solution if the exception is thrown only occasionally. `try` is not very costly, `catch` is. @Elrod kindly provided a lot of useful benchmarks there.
