Safe to reusing the same Exception instance?

This question might be a bit technical with regards to the inner
workings
of Ruby. (I considered posint to ruby-core) but hopefully here someone
will
know.

Whittling it down to the essentials, I am wondering if it is safe to
raise
the same error instance multiple times.

$ANERROR = RuntimeError.new

def foo(var)
  begin
    raise $ANERROR, "dynamic message here #{var}" if var.says_so?
  rescue
    recorded_errors << var
  end
end

The function in itself doesn’t matter, that’s just a silly example. The
main thing is the re-raising of a single instance of the error class, in
this example $ANERROR.

Yea, subject should say “reuse”.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs