`try/catch` statements in Pluto

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:

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?

try introduces local scope (independently of Pluto):

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.

3 Likes

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

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

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.

1 Like

That is also quite elegant. Thanks a lot!

1 Like

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