# Try catch question

**URL:** <https://discourse.julialang.org/t/try-catch-question/52384>\
**Category:** New to Julia\
**Tags:** json\
**Created:** [December 25, 2020, 9:52pm UTC](https://discourse.julialang.org/t/try-catch-question/52384 "2020-12-25T21:52:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jojo\_Dad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jojo_dad/32/8620_2.png) [@Jojo\_Dad](https://discourse.julialang.org/u/Jojo_Dad)\
**Post date:** [December 25, 2020, 9:52pm UTC](https://discourse.julialang.org/t/try-catch-question/52384/1 "2020-12-25T21:52:25Z")

</div>

I use

```julia
    try
          JSON.parsefile(fname) do stuff
             content = "Nothing ..."          
          end
     catch
         content = "File "*fname*" not found! ";
     end

```

But it always returns that file was not found despite that JSON.parsefile(fname) works …

---

<div class="post-metadata">

**Author:** ![Jojo\_Dad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jojo_dad/32/8620_2.png) [@Jojo\_Dad](https://discourse.julialang.org/u/Jojo_Dad)\
**Post date:** [December 25, 2020, 10:27pm UTC](https://discourse.julialang.org/t/try-catch-question/52384/3 "2020-12-25T22:27:02Z")

</div>

It should be now…

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [December 26, 2020, 1:32am UTC](https://discourse.julialang.org/t/try-catch-question/52384/4 "2020-12-26T01:32:02Z")

</div>

You seem to be confused about what the `do` syntax does. [Manual entry for `do`](https://docs.julialang.org/en/v1/base/base/#do). This

```julia
JSON.parsefile(fname) do stuff
    content = "Nothing ..."          
end

```

is equivalent to

```julia
JSON.parsefile(stuff -> content = "Nothing ...", fname)

```

i.e. the `do` syntax invokes the function (`JSON.parsefile`) with an anonymous function as the first argument. Since there is no method of `JSON.parsefile` that accepts a function as first argument this will always be a `MethodError` and you will always end up in the `catch` block. Indeed, if you had tried just the code in your `try` block you would have seen this.

---

<div class="post-metadata">

**Author:** ![MA\_Laforge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ma_laforge/32/385_2.png) [@MA\_Laforge](https://discourse.julialang.org/u/MA_Laforge)\
**Post date:** [December 26, 2020, 2:47am UTC](https://discourse.julialang.org/t/try-catch-question/52384/5 "2020-12-26T02:47:37Z")

</div>

To elaborate a bit more on @fredrikekre’s comment:

```julia
JSON.parsefile(fname) do stuff
    content = "Nothing ..."          
end

```

This statement would need to call a version of `JSON.parsefile()` that defines:

```julia
parsefile(function_to_do::Function, filename::AbstractString, [other optional arguments here])

```

And that version of `parsefile()` would need to correctly call (expect) the anonymous function you defined. In other words, it would have to call:

```julia
function_to_do(stuff)

```

somewhere in its code. This does not happen by magic. Someone has to implement this version of the function. But if you check the methods defined in `JSON`, you will notice that only one method exists, and it does not have `::Function` as its first argument:

```julia
julia> methods(JSON.parsefile)
# 1 method for generic function "parsefile":
[1] parsefile(filename::AbstractString; dicttype, inttype, null, allownan, use_mmap) in JSON.Parser at /home/alain/.julia/packages/JSON/3rsiS/src/Parser.jl:518

```

---

<div class="post-metadata">

**Author:** ![MA\_Laforge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ma_laforge/32/385_2.png) [@MA\_Laforge](https://discourse.julialang.org/u/MA_Laforge)\
**Post date:** [December 26, 2020, 2:56am UTC](https://discourse.julialang.org/t/try-catch-question/52384/6 "2020-12-26T02:56:22Z")

</div>

### The `try` pitfall

When you get unexpected behaviour like this, I highly recommend commenting out the `try/catch/end` block to see what is happening. Normally, Julia’s exception system would have shown you a helpful error message, but it gets suppressed by your `try/catch` block:

```julia-auto
ERROR: MethodError: no method matching parsefile(::var"#7#8", ::String)
Stacktrace:
 [1] top-level scope at REPL[8]:1

```

Now that you understand how `function()-do` statements work, you can see that Julia cannot find a version of `parsefile()` that accepts a `::Function` as its first argument (even if it displays a message with the somewhat cryptic `::var"#7#8"` type in Julia v1.3.1).
