Accessing the last error raised outside of a rescue block

D

Doug Livesey

Hi -- I want to be able to grab some details from an error, but not just
the message.
So I want a variable called result to be either the result of a method,
or a property of the error raised by that method.
If the property I wanted from the error was the message, then I could do
this like this:
result = my_method rescue $!

However, what I want to do is call a method on the error, like so:
result = my_method rescue last_error.my_other_method

Where last_error is the mysterious means I employ to get at the error
raised, and my_other_method is what I want to call on it.

Does anyone know if this is possible?
Cheers,
Doug.
 
P

Phlip

Doug said:
result = my_method rescue $!

However, what I want to do is call a method on the error, like so:
result = my_method rescue last_error.my_other_method

Where last_error is the mysterious means I employ to get at the error
raised, and my_other_method is what I want to call on it.

The last time I tried to do anything with a rescue statement modifier other than
replace its statements value, it didn't work. However...

result = my_method rescue $!.my_other_method

Does that work?
 
D

Doug Livesey

Yup, that worked beautifully, cheers!
Now the scary question is "why on earth didn't I try that?"
I've whittled it down to two possible answers, and the good news for you
is that you are a genius, because the alternative is that I'm a maroon,
and I'm not having that.
Cheers, man!
Doug.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top