# Try/catch question

**URL:** <https://discourse.julialang.org/t/try-catch-question/15190>\
**Category:** New to Julia\
**Created:** [September 19, 2018, 6:27pm UTC](https://discourse.julialang.org/t/try-catch-question/15190 "2018-09-19T18:27:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)\
**Post date:** [September 19, 2018, 6:27pm UTC](https://discourse.julialang.org/t/try-catch-question/15190/1 "2018-09-19T18:27:42Z")

</div>

I’m trying to use a try/catch block to debug something that is failing. The code looks something like

```
df = DataFrame(...)
for row in eachrow(df)
    try
        variable = row[:1]
        ....(some code)....
    catch
        println(variable)
    end
end

```

When I do this, the variable printed in the catch block appears to not be what is in set in the try block. Is this the expected behavior?

---

<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:** [September 19, 2018, 6:45pm UTC](https://discourse.julialang.org/t/try-catch-question/15190/2 "2018-09-19T18:45:06Z")

</div>

Why not make a minimal **working** example that shows the unexpected behavior? It makes things much easier.

---

<div class="post-metadata">

**Author:** ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)\
**Post date:** [September 19, 2018, 7:08pm UTC](https://discourse.julialang.org/t/try-catch-question/15190/3 "2018-09-19T19:08:53Z")

</div>

Sorry, I thought it would be a simple “you’re doing it wrong” kind of answer…

Here is my code

```julia
df = DataFrame(col1=[1,2,3],col2=[6,7,8])
var = 999
for row in eachrow(df)
    try
        var = row[:1]
        println("try $var")
        if var > 2 
            throw(ErrorException("test"))
        end

    catch
        println("catch $var")
    end
end

```

Here are the results

```
try 1
try 2
try 3
catch 999

```

Shouldn’t it be:

```
try 1
try 2
try 3
catch 3

```

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [September 19, 2018, 7:14pm UTC](https://discourse.julialang.org/t/try-catch-question/15190/4 "2018-09-19T19:14:11Z")

</div>

`try` and `catch` each have their own scope, and local variables aren’t implicitly visible between scopes. Instead, mark the shared variable as `local` to the parent scope:

```julia
using DataFrames
df = DataFrame(col1=[1,2,3],col2=[6,7,8])
var = 999
for row in eachrow(df)
    local var
    try
        var = row[:1]
        println("try $var")
        if var > 2 
            throw(ErrorException("test"))
        end

    catch
        println("catch $var")
    end
end

```

---

<div class="post-metadata">

**Author:** ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)\
**Post date:** [September 19, 2018, 8:16pm UTC](https://discourse.julialang.org/t/try-catch-question/15190/5 "2018-09-19T20:16:32Z")

</div>

Thanks, I appreciate the help.

Now I accept that this is the answer, but this bothers me a bit. This is really one block statement with one END statement isn’t it? So I would have thought that these are part of the same scope. It seems unintuitive to me that they have different scopes.

Other languages like python don’t seem to behave this way as far as I know.

I’m really trying to migrate to Julia, because Python syntax just annoys me, but the barrier to entry seems quite high. I wish there was some “Dummy” or “Easy” mode that people like me could use that would operate in a simpler, more intuitive manner, even if the performance were degraded a bit.

Anyways, onward…

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [September 19, 2018, 8:58pm UTC](https://discourse.julialang.org/t/try-catch-question/15190/6 "2018-09-19T20:58:54Z")

</div>

Fortunately, this mode [exists](https://github.com/stevengj/SoftGlobalScope.jl)!

```julia
using DataFrames, SoftGlobalScope
df = DataFrame(col1=[1,2,3],col2=[6,7,8])
var = 999
@softscope for row in eachrow(df)
    try
        var = row[:1]
        println("try $var")
        if var > 2 
            throw(ErrorException("test"))
        end

    catch
        println("catch $var")
    end
end

```

---

<div class="post-metadata">

**Author:** ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)\
**Post date:** [September 19, 2018, 9:04pm UTC](https://discourse.julialang.org/t/try-catch-question/15190/7 "2018-09-19T21:04:05Z")

</div>

Great!

As the self appointment representative of all of the dummies out there, I thank you and the author of this package.

---

<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:** [September 19, 2018, 9:27pm UTC](https://discourse.julialang.org/t/try-catch-question/15190/8 "2018-09-19T21:27:34Z")

</div>

As has been discussed [here](https://github.com/JuliaLang/julia/issues/28789) this package, in some form, will become the default in all interactive julia environments (including the REPL). **We are all idots! 🙂**
