# \`try\` blocks, improving their docs

**URL:** <https://discourse.julialang.org/t/try-blocks-improving-their-docs/21917>\
**Category:** Internals & Design\
**Created:** [March 15, 2019, 4:03pm UTC](https://discourse.julialang.org/t/try-blocks-improving-their-docs/21917 "2019-03-15T16:03:57Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [March 15, 2019, 4:03pm UTC](https://discourse.julialang.org/t/try-blocks-improving-their-docs/21917/1 "2019-03-15T16:03:57Z")

</div>

Let’s document the in’s and out’s of `try [catch] [finally] end` blocks, and the use[s] of each part, the purpose of `rethrow` and why one would choose to include or exclude either the `catch` part or the `finally` part or both. Others know this part of Julia much better than I … getting the correct info here is reasonable way to migrate it into the docs.

---

<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:** [March 15, 2019, 4:11pm UTC](https://discourse.julialang.org/t/try-blocks-improving-their-docs/21917/2 "2019-03-15T16:11:01Z")

</div>

What are you missing from

[https://docs.julialang.org/en/v1/manual/control-flow/#Exception-Handling-1](https://docs.julialang.org/en/v1/manual/control-flow/#Exception-Handling-1)

?

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [March 15, 2019, 4:33pm UTC](https://discourse.julialang.org/t/try-blocks-improving-their-docs/21917/3 "2019-03-15T16:33:09Z")

</div>

pictures with flow arrows and contextual conditions

---

<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:** [March 15, 2019, 4:56pm UTC](https://discourse.julialang.org/t/try-blocks-improving-their-docs/21917/4 "2019-03-15T16:56:03Z")

</div>

> [@JeffreySarnoff](#):
>
> contextual conditions

Can you explain what you mean by this? This is the first time I encounter the term.

FWIW, I find this part of the documentation pretty clear, and I think it has examples for the typical patterns one encounters.

Regarding pictures: the manual is pretty thin on these, as they are labor-intensive to create and maintain. I am not sure that there is a compelling need for them here, but perhaps I missed something that is better explained visually.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [March 15, 2019, 6:36pm UTC](https://discourse.julialang.org/t/try-blocks-improving-their-docs/21917/5 "2019-03-15T18:36:31Z")

</div>

sometimes a picture is easier to see than are words to comprehend

 ![trycatchfinally1](https://global.discourse-cdn.com/julialang/original/3X/a/6/a6d59cc5c6515221a616d944fd4d918121746a75.png)

---

<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:** [March 15, 2019, 6:53pm UTC](https://discourse.julialang.org/t/try-blocks-improving-their-docs/21917/6 "2019-03-15T18:53:31Z")

</div>

Sometimes it is, but it would be great if you could make _specific_ suggestions about what you are missing in that part of the manual.

So far, with 3 posts into this thread, it is not very clear.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [March 15, 2019, 6:59pm UTC](https://discourse.julialang.org/t/try-blocks-improving-their-docs/21917/7 "2019-03-15T18:59:09Z")

</div>

If I understood better what I have yet to understand about this, I could be of more direct help (the genesis of this thread is [Lost variable assignment in nested try/catch/finally blocks · Issue #31357 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/31357)).  
I thought that others who had worked some of the more subtle aspects of `try, throw, catch, rethrow, finally` in their parts as much as as a whole approach [which is familiar] could jot a few notes and in the aggregate we could make the docs more fully and easily understood and applied by others who may be new the constructs.

---

<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:** [March 15, 2019, 7:13pm UTC](https://discourse.julialang.org/t/try-blocks-improving-their-docs/21917/8 "2019-03-15T19:13:20Z")

</div>

> [@JeffreySarnoff](#):
>
> (the genesis of this thread is [https://github.com/JuliaLang/julia/issues/31357](https://github.com/JuliaLang/julia/issues/31357))

Not sure what is relevant from that issue, it seems to be about an optimizer bug.

You seem to be making the implicit assumption that there is some expert knowledge missing from the docs without being able to articulate what it is. I wonder why.

I would propose that there isn’t anything essential that is missing from the docs, and major common patterns are rather well-documented. Perhaps an extra example with `rethrow` could help (that is a common pattern for catching just a subset of exceptions), but that’s about it. I see no particular place for graphical illustrations.

Exceptions are in fact not an area where Julia differs from most modern languages with a REPL, if is fairly standard fare.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [March 15, 2019, 7:14pm UTC](https://discourse.julialang.org/t/try-blocks-improving-their-docs/21917/9 "2019-03-15T19:14:33Z")

</div>

ok … no worries

---

<div class="post-metadata">

**Author:** ![tjgreen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tjgreen/32/6749_2.png) [@tjgreen](https://discourse.julialang.org/u/tjgreen)\
**Post date:** [March 15, 2019, 8:05pm UTC](https://discourse.julialang.org/t/try-blocks-improving-their-docs/21917/10 "2019-03-15T20:05:08Z")

</div>

The section on `finally` does a good job of motivating the feature, but describes the semantics a bit too quickly. I’d like to see a fuller explanation of when, exactly, the `finally` clause is executed, in a variety of situations (e.g. more complicated ones involving nested `try/catch/finally` blocks). Also, what happens, exactly, if the `finally` clause itself throws an exception?

---

<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:** [March 16, 2019, 6:29am UTC](https://discourse.julialang.org/t/try-blocks-improving-their-docs/21917/11 "2019-03-16T06:29:55Z")

</div>

> [@tjgreen](#):
>
> Also, what happens, exactly, if the `finally` clause itself throws an exception?

Good question — it looks like that replaces the other error, which I think makes sense:

```julia
function f(x)
    try
        x ≥ 1 && throw(error("too much"))
        x
    finally
        @info "finalizing"
        x ≥ 2 && throw(error("insidious bug"))
    end
end

```

gives

```julia
julia> f(0)
[ Info: finalizing
0

julia> f(1)
[ Info: finalizing
ERROR: too much
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] f(::Int64) at ./REPL[115]:3
 [3] top-level scope at none:0

julia> f(2)
[ Info: finalizing
ERROR: insidious bug
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] f(::Int64) at ./REPL[115]:7
 [3] top-level scope at none:0

```
