# Failure to catch an exception

**URL:** <https://discourse.julialang.org/t/failure-to-catch-an-exception/12822>\
**Category:** New to Julia\
**Created:** [August 2, 2018, 12:01pm UTC](https://discourse.julialang.org/t/failure-to-catch-an-exception/12822 "2018-08-02T12:01:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [August 2, 2018, 12:01pm UTC](https://discourse.julialang.org/t/failure-to-catch-an-exception/12822/1 "2018-08-02T12:01:15Z")

</div>

Hi!

I am getting into exception handling, but I am surprised at how my code works. Note that I am on Julia 0.6 (will port to 1.0 as soon as it’s out!). So, here goes:

```julia
function bungle(c)
    out = 0
    try
        @printf "Bungling away\n"
        out = 1
        if c == 1
            @assert false @sprintf "We have a main %s undervolt" "B"
        elseif c==2
            M = spzeros(1e4)
            v = randn(1e4)
            x = M\v
        elseif c==3
            a = sqrt(-1.)
        elseif c==4
            throw(DomainError())
        end    
    catch ex
        out = 2
        @printf "\nHouston we have a problem:\n%s\n" ex.msg
    finally
        @printf "Shutting down.\n"
        return out
    end
end # function bungle

@show bungle(0)

```

Calling bungle(0) and bungle(1) behaves as I intend. Bungle(2), bungle(3) and bungle(4) throw exceptions, which should be caught and reported. However, in these cases, the catch block is _not executed_, while the finaly block is. Why on earth!?!

🤨

Philippe

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [August 2, 2018, 1:57pm UTC](https://discourse.julialang.org/t/failure-to-catch-an-exception/12822/2 "2018-08-02T13:57:11Z")

</div>

The reason is `return` statement in `finally` block, which does not allow an exception from `try` or `catch` block to propagate.

I am not a core dev, but I would say it is a bug, as the documentation says:

> If the try block exits due to an exception, the exception will continue propagating

but we have the following behavior (I would say this is a MWE of the problem):

```julia
julia> function h()
           try
               sqrt(-1)
           finally
               return 1
           end
       end
h (generic function with 1 method)

julia> h()
1

```

and exception does not propagate. If this is intended then it is not documented.

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [August 2, 2018, 2:20pm UTC](https://discourse.julialang.org/t/failure-to-catch-an-exception/12822/3 "2018-08-02T14:20:34Z")

</div>

Thanks, you are right.

This has to be a bug. I cannot see how the combination of return statement and _some_ exception types can make it logical not to execute the catch block.

I will plagiate your code and report this as a bug.

I will find some workaround: the reason I catch exceptions is I want to return whatever the computations have achieved (the steps that converged)…

Cheers

Philippe

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [August 2, 2018, 2:27pm UTC](https://discourse.julialang.org/t/failure-to-catch-an-exception/12822/4 "2018-08-02T14:27:55Z")

</div>

But in simpler MWE catch block is executed

```julia
julia> function h()
    try
        sqrt(-1)
    catch e
        println("Houston!")
    finally return 1
    end
end;

julia> h()
Houston!
1

```

BTW your example prints error under 0.7.0-rc1.15

```julia
0.7.0-rc1.15> @show bungle(3)
Bungling away

Houston we have a problem:
sqrt will only return a complex result if called with a complex argument. Try sqrt(Complex(x)).
Shutting down.
bungle(3) = 2
2

```

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [August 2, 2018, 2:47pm UTC](https://discourse.julialang.org/t/failure-to-catch-an-exception/12822/5 "2018-08-02T14:47:37Z")

</div>

In all examples `catch` block is executed. The problem is that when an exception in `catch` block is thrown then it is not passed through if there is a `return` statement in `finally`.

And the specific problem is in the original code that not all exceptions have `msg` field. Then the Julia throws error that is not passed through later. But the core problem is what I have explained in my MWE (without `catch`). If in this MWE you move out `return` past `finally` error is raised:

```julia
julia> function h()
           try
               sqrt(-1)
           finally
               println("Finally")
           end
           return 1
       end
h (generic function with 1 method)
julia> h()
Finally
ERROR: DomainError:
Stacktrace:
 [1] h() at .\REPL[4]:3

```

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [August 2, 2018, 3:02pm UTC](https://discourse.julialang.org/t/failure-to-catch-an-exception/12822/6 "2018-08-02T15:02:05Z")

</div>

Thanks for adding this explanation! 🙂

In Julia 0.6.1 I got this:

```julia
julia> @show bungle(3)
Bungling away
Shutting down.
bungle(3) = 2
2

```

compare it to what I wrote before about 0.7.0. I didn’t analyze it deeply but it seemed to me that in 0.6.1 printf to Houston was not processed. (I could be wrong).

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [August 2, 2018, 3:11pm UTC](https://discourse.julialang.org/t/failure-to-catch-an-exception/12822/7 "2018-08-02T15:11:16Z")

</div>

> [@Liso](#):
>
> I didn’t analyze it deeply

The reason is that `DomainError` has field `msg` in Julia 0.7 and it does not have it in Julia 0.6. So in Julia 0.7 no error is thrown in `catch` clause and all is processed cleanly.

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [August 2, 2018, 3:13pm UTC](https://discourse.julialang.org/t/failure-to-catch-an-exception/12822/8 "2018-08-02T15:13:48Z")

</div>

That’s it, you got it: I bugged my catch! Julia goes free.

Thank you, now this will be easy to correct.

: )

Philippe

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [August 2, 2018, 7:26pm UTC](https://discourse.julialang.org/t/failure-to-catch-an-exception/12822/9 "2018-08-02T19:26:12Z")

</div>

> [@Philippe\_Maincon1](#):
>
> I will plagiate your code and report this as a bug.

Maybe core-devs can comment here before we file an issue?

The question is whether `return` statement in `finally` clause should suppress propagation of an exception thrown in:

- `catch` block
- `try` block when there is no `catch` block

CC @jeff.bezanson

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [August 3, 2018, 4:38am UTC](https://discourse.julialang.org/t/failure-to-catch-an-exception/12822/10 "2018-08-03T04:38:20Z")

</div>

Issues are best handled on Github.  
It has the right tools for the job,  
In terms of cross-referencing; devs getting notifications,  
closing, etc etc.

Just open an issue.  
It may be closed, it may not.  
But trust me no one will get mad for creating an issue prematurely without discussing it with them on discourse first.
