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


#1

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


#2

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 - mój blog


#3

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

2009/1/11 Radosław Bułat removed_email_address@domain.invalid


#4

2009/1/11 Greg H. 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 - mój blog


#5

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


#6

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://eigenclass.org/hiki/method+arguments+via+introspection