For example, if I have a function

```
function f(x::Int)
if x == 0
throw(DivideError())
return 2.0 / x
end
```

What return type should I specify at the end of `f(x::Int)`

? Should I consider `Union{DivideError, Float64}`

or just `Float64`

?

I know there is a package ResultTypes.jl that can handle this. But is it necessary to use it?

Throwing an error is not the same as returning an error. This function can only return a `Float64`

or not return at all:

```
julia> function f(x::Int)
if x == 0
throw(DivideError())
end
return 2.0/x
end
f (generic function with 1 method)
julia> @code_warntype f(1)
Body::Float64
2 1 ─ %1 = (x === 0)::Bool
└── goto #3 if not %1
3 2 ─ (Main.throw)($(QuoteNode(DivideError())))
└── $(Expr(:unreachable))
5 3 ─ %5 = (Base.sitofp)(Float64, x)::Float64
│ %6 = (Base.div_float)(2.0, %5)::Float64
└── return %6
```

2 Likes