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

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

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

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

···

2009/1/11 Radosław Bułat <radek.bulat@gmail.com>

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

--
Greg
http://blog.gregnet.org/

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.

···

2009/1/11 Greg Hauptmann <greg.hauptmann.ruby@gmail.com>:

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

--
Pozdrawiam

Radosław Bułat
http://radarek.jogger.pl - mój blog

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

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 <radek.bulat@gmail.com>

2009/1/11 Greg Hauptmann <greg.hauptmann.ruby@gmail.com>:
> 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

--
Greg
http://blog.gregnet.org/