# Accessing the last error raised outside of a rescue block

**URL:** <https://www.ruby-forum.com/t/accessing-the-last-error-raised-outside-of-a-rescue-block/143421>\
**Category:** Ruby\
**Created:** [July 18, 2008, 11:24am UTC](https://www.ruby-forum.com/t/accessing-the-last-error-raised-outside-of-a-rescue-block/143421 "2008-07-18T11:24:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![biot023](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7ab992/32.png) [@biot023](https://www.ruby-forum.com/u/biot023)\
**Post date:** [July 18, 2008, 11:24am UTC](https://www.ruby-forum.com/t/accessing-the-last-error-raised-outside-of-a-rescue-block/143421/1 "2008-07-18T11:24:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![biot023](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7ab992/32.png) [@biot023](https://www.ruby-forum.com/u/biot023)\
**Post date:** [July 18, 2008, 12:52pm UTC](https://www.ruby-forum.com/t/accessing-the-last-error-raised-outside-of-a-rescue-block/143421/2 "2008-07-18T12:52:31Z")

</div>

Doug L. wrote:

> 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?

---

<div class="post-metadata">

**Author:** ![biot023](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7ab992/32.png) [@biot023](https://www.ruby-forum.com/u/biot023)\
**Post date:** [July 18, 2008, 3:15pm UTC](https://www.ruby-forum.com/t/accessing-the-last-error-raised-outside-of-a-rescue-block/143421/3 "2008-07-18T15:15:56Z")

</div>

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.
