Does Ruby’s “raise” modify exception?
Or, are following snippets:
some_method(MyException.new)
and
begin
raise MyException.new
rescue MyException => e
some_method(e)
end
equivalent? If not, what are the differences?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, they are not equivalent.
gives
Does anyone know other differences?
EDIT:
Kernel#raiseWILL NOT modify exception unlessraiseis called with no params and$!is defined (that means: except re-raising exception inrescueclause with no params). In following example:exception will be modified (its context and backtrace altered) in #1 raise but untouched in #2 raise. It WILL be modified in any other case, including re-raising exception with some param like:
Here exception will be altered in both #1 and #2.
See https://github.com/rubinius/rubinius/blob/master/kernel/delta/kernel.rb for details.
Above applies to Rubinius. It should be the same in MRI 1.9 and JRuby, but I haven’t checked it.