# Type stability when a branch can throw an exception

**URL:** <https://discourse.julialang.org/t/type-stability-when-a-branch-can-throw-an-exception/45223>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 19, 2020, 6:25pm UTC](https://discourse.julialang.org/t/type-stability-when-a-branch-can-throw-an-exception/45223 "2020-08-19T18:25:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [August 19, 2020, 6:25pm UTC](https://discourse.julialang.org/t/type-stability-when-a-branch-can-throw-an-exception/45223/1 "2020-08-19T18:25:20Z")

</div>

Is a function considered type stable if it has a branch that can throw an exception? Here’s an example:

```julia
function foo(x::Int)
    if x < 0
        error("Oops.")
    else
        2x
    end
end

```

`@code_warntype` doesn’t seem to have a problem with it:

```julia
julia> @code_warntype foo(-3)
Variables
  #self#::Core.Compiler.Const(foo, false)
  x::Int64

Body::Int64
1 ─ %1 = (x < 0)::Bool
└── goto #3 if not %1
2 ─ Main.error("Oops.")
└── Core.Compiler.Const(:(return %3), false)
3 ┄ %5 = (2 * x)::Int64
└── return %5

```

Considering that any function called inside a branch could conceivably throw an exception, I suppose it would be bad if the possibility of an exception interfered with type inference…

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [August 19, 2020, 9:04pm UTC](https://discourse.julialang.org/t/type-stability-when-a-branch-can-throw-an-exception/45223/2 "2020-08-19T21:04:26Z")

</div>

Yes, this is type stable. Type instabilities are only when the return type can’t be inferred properly. In this case, `throw` doesn’t return, it just halts the execution.

```julia
julia> Base.return_types(foo, tuple(Int))
1-element Array{Any,1}:
 Int64

```
