# How can I access after an exception (in the rescue area) the file, class, method, line number for wh

**URL:** <https://www.ruby-forum.com/t/how-can-i-access-after-an-exception-in-the-rescue-area-the-file-class-method-line-number-for-wh/157435>\
**Category:** Ruby\
**Created:** [January 11, 2009, 9:33am UTC](https://www.ruby-forum.com/t/how-can-i-access-after-an-exception-in-the-rescue-area-the-file-class-method-line-number-for-wh/157435 "2009-01-11T09:33:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [January 11, 2009, 9:33am UTC](https://www.ruby-forum.com/t/how-can-i-access-after-an-exception-in-the-rescue-area-the-file-class-method-line-number-for-wh/157435/1 "2009-01-11T09:33:29Z")

</div>

Hi,  
How can I access after an exception (in the rescue area) the following  
information do you know (i.e. associated with where the error occured)?

- file,
- line number
- class
- method

thanks

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [January 11, 2009, 12:30pm UTC](https://www.ruby-forum.com/t/how-can-i-access-after-an-exception-in-the-rescue-area-the-file-class-method-line-number-for-wh/157435/2 "2009-01-11T12:30:55Z")

</div>

When you catch, err, rescue exception look at the ‘backtrace’ method.  
It won’t give you all information you need, but some of them.

# file exc.rb

class A  
def foo  
raise “bar”  
end  
end

begin  
a = A.new  
a.foo  
rescue =\> e  
puts e.backtrace  
end

$ ruby exc.rb  
exc.rb:3:in `foo’  
exc.rb:9

–  
Pozdrawiam

Rados³aw Bu³at  
[http://radarek.jogger.pl](http://radarek.jogger.pl) - mój blog

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [January 11, 2009, 12:46pm UTC](https://www.ruby-forum.com/t/how-can-i-access-after-an-exception-in-the-rescue-area-the-file-class-method-line-number-for-wh/157435/3 "2009-01-11T12:46:44Z")

</div>

thanks - no way to get the class name & method name then?

2009/1/11 RadosÅ‚aw BuÅ‚at [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [January 11, 2009, 2:17pm UTC](https://www.ruby-forum.com/t/how-can-i-access-after-an-exception-in-the-rescue-area-the-file-class-method-line-number-for-wh/157435/4 "2009-01-11T14:17:15Z")

</div>

2009/1/11 Greg H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> thanks - no way to get the class name & method name then?

Method name is included in backtrace. e.backtrace returns array of  
string, where each string has following form:  
file:line in `method’  
You can try to parse it (simply with regexp). I’m surprised that there  
is no information about class but probably there is reason for that.

–  
Pozdrawiam

Rados³aw Bu³at  
[http://radarek.jogger.pl](http://radarek.jogger.pl) - mój blog

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [January 11, 2009, 9:39pm UTC](https://www.ruby-forum.com/t/how-can-i-access-after-an-exception-in-the-rescue-area-the-file-class-method-line-number-for-wh/157435/5 "2009-01-11T21:39:58Z")

</div>

thanks - wonder if given you have the method name, filename, line  
number,  
whether there is a way to work out the class you’re in? If it comes  
down to  
having to parse through the file itself it may get a bit messy  
however…

2009/1/11 RadosÅ‚aw BuÅ‚at [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [January 12, 2009, 7:29pm UTC](https://www.ruby-forum.com/t/how-can-i-access-after-an-exception-in-the-rescue-area-the-file-class-method-line-number-for-wh/157435/6 "2009-01-12T19:29:18Z")

</div>

> no way to get class name [of exception backtraces].

There may be some room to make this better in 1.9, but for now you have  
to [ugh]  
reparse or do whatever’s necessary.

Some possibly helpful links:  
[http://github.com/rogerdpack/ruby\_backtracer/tree/master](http://github.com/rogerdpack/ruby_backtracer/tree/master)  
[http://eigenclass.org/hiki/method+arguments+via+introspection](http://eigenclass.org/hiki/method+arguments+via+introspection)
