What's the differences between `throw(ArgumentError(""))` and `return throw(ArgumentError("")`)

i wrote a function that will throw an ArgumentError if it occurred any exception.
but i have a question about throwing an error:
i can use throw() directly, also i can use the return keyword.
Is there any difference?

function distance(a, b)
    if length(a) != length(b)
        return throw(ArgumentError(""))
    end
    dist = 0
    i = 1
    while i <= length(a)
        if a[i] != b[i]
            dist += 1
        end
        i += 1
    end
    return dist
end
1 Like

Here the return keyword is useless and misleading. The following:

function f()
    return throw(...)
end

is a bit like

function f()
    return sin("abc")   # Some invalid code that throws an error
end

In both cases, Julia is supposed to evaluate an expression and return the result. But in both cases, an exception is thrown during evaluation, so the execution is stopped before the function returns anything.

4 Likes