A Julia equivalent to R's stop()?

You could probably do something like:

struct Stop{T}
    answer::T
end

function stop(answer)
    throw(Stop(answer))
end

function start()
    stop(42)
end

try
    start()
    @info "No answer."
catch ex
    if isa(ex, Stop)
        @info "Exit with: $(ex.answer)"
    else
        rethrow(ex)
    end
end

This assumes that you are not already catching exceptions in the stack. If you are then you probably want to throw a “Stop” object that the other exception handlers can rethrow.

This is also not the approved way of unwinding the stack, throw is meant for errors not a normal exit.

3 Likes