Rescue Exception => ex

I am trying to grok
rescue MySpecialError => ex

Does the
=>
have anything to do with hashes or is this syntax reserved for rescue
clauses?

On Sun, Jan 15, 2012 at 20:42, Ralph S. [email protected] wrote:

rescue MySpecialError => ex

Does the
=>
have anything to do with hashes or is this syntax reserved for rescue
clauses?

It’s a fair question, since the “=>” is shared between the two. However,
if
this syntax were actually a hash, it would be getting passed to rescue()
as
a method, but ex would be seen as a local variable. Explicitly:

begin
raise StandardError
rescue({StandardError => e})
p e
end

NameError: undefined local variable or method `e’ for main:Object

or

begin
raise StandardError
rescue(StandardError => e)
p e
end

SyntaxError: (irb):8: syntax error, unexpected tASSOC, expecting ‘)’

rescue(StandardError => e)

If Adam’s response only confused you further: yes, it’s a special
case, reserved for rescue clauses - it assigns the exception raised to
the ex variable.

– Matma R.

Monday, January 16, 2012, 11:51:05 AM, you wrote:

BD> If Adam’s response only confused you further: yes, it’s a special
BD> case, reserved for rescue clauses - it assigns the exception raised
to
BD> the ex variable.

BD> – Matma R.

Well, I’m guessing it caused him to scratch his head and prove to
himself (and me) that it was unrelated to hashes.

I thank you for the clearer answer that “It’s a special case.”

Anyone point out in the documentation where it says that or is it
something “you just have to know.”?

On Mon, Jan 16, 2012 at 19:57, Ralph S. [email protected] wrote:

Well, I’m guessing it caused him to scratch his head and prove to himself
(and me) that it was unrelated to hashes.

It didn’t cause me to scratch my head, I just figure it’s always good to
have a simple demonstration of something in code whenever possible. :slight_smile:

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