# \`try/catch\` statements in Pluto

**URL:** <https://discourse.julialang.org/t/try-catch-statements-in-pluto/66835>\
**Category:** Pluto\
**Created:** [August 23, 2021, 11:26am UTC](https://discourse.julialang.org/t/try-catch-statements-in-pluto/66835 "2021-08-23T11:26:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [August 23, 2021, 11:26am UTC](https://discourse.julialang.org/t/try-catch-statements-in-pluto/66835/1 "2021-08-23T11:26:40Z")

</div>

EDIT: There was no problem, I was simply not aware that a `try/catch` statement introduces a local scope. Pluto and reactivity are solid \<3

By using a try/catch statement in a pluto notebook, I can define a variable `test` two times:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/7/27027d63fea0260d6ee62c4f411ede400f58f5c9.png)

It is clear that the definition inside the try statement actually has no effect, despite not erroring.

I guess that try/catch is a nasty case to handle for interactivity. But the current way things work is silently not working, which I think we all agree is the worst way to handle anything.

Can Pluto handle try/catch statements? Or should they throw a warning in Pluto notebooks?

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [August 23, 2021, 11:36am UTC](https://discourse.julialang.org/t/try-catch-statements-in-pluto/66835/2 "2021-08-23T11:36:15Z")

</div>

`try` introduces local scope (independently of Pluto):

```julia
julia> try
           test = 2
       catch
          nothing
       end
2

julia> test
ERROR: UndefVarError: test not defined

```

so the `test` inside the `try` is just like a local variable `test` inside a `for` loop-- it doesn’t affect the global scope.

---

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [August 23, 2021, 11:52am UTC](https://discourse.julialang.org/t/try-catch-statements-in-pluto/66835/3 "2021-08-23T11:52:21Z")

</div>

Alright, that makes sense. So adding a `global` before the definition should make things work?

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [August 23, 2021, 12:14pm UTC](https://discourse.julialang.org/t/try-catch-statements-in-pluto/66835/4 "2021-08-23T12:14:56Z")

</div>

Depending what you want, that could work. You could also do something like

```julia
test = try
           2
       catch
           nothing
       end

```

so that `test` is not defined within the `try` block, but rather is a global whose value is given by whatever the `try` block evaluates to.

---

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [August 23, 2021, 3:01pm UTC](https://discourse.julialang.org/t/try-catch-statements-in-pluto/66835/5 "2021-08-23T15:01:45Z")

</div>

That is also quite elegant. Thanks a lot!

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [August 24, 2021, 6:25am UTC](https://discourse.julialang.org/t/try-catch-statements-in-pluto/66835/6 "2021-08-24T06:25:33Z")

</div>

You can also simply put `local test` in front of the try catch block.
