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

G

Greg Hauptmann

[Note: parts of this message were removed to make it a legal post.]

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
 
R

Rados³aw Bu³at

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 =3D A.new
a.foo
rescue =3D> e
puts e.backtrace
end

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


--=20
Pozdrawiam

Rados=B3aw Bu=B3at
http://radarek.jogger.pl - m=F3j blog
 
G

Greg Hauptmann

dGhhbmtzIC0gbm8gd2F5IHRvIGdldCB0aGUgY2xhc3MgbmFtZSAmIG1ldGhvZCBuYW1lIHRoZW4/
CgoyMDA5LzEvMTEgUmFkb3OzYXcgQnWzYXQgPHJhZGVrLmJ1bGF0QGdtYWlsLmNvbT4KCj4gV2hl
biB5b3UgY2F0Y2gsIGVyciwgcmVzY3VlIGV4Y2VwdGlvbiBsb29rIGF0IHRoZSAnYmFja3RyYWNl
JyBtZXRob2QuCj4gSXQgd29uJ3QgZ2l2ZSB5b3UgYWxsIGluZm9ybWF0aW9uIHlvdSBuZWVkLCBi
dXQgc29tZSBvZiB0aGVtLgo+Cj4gIyBmaWxlIGV4Yy5yYgo+IGNsYXNzIEEKPiAgZGVmIGZvbwo+
ICAgIHJhaXNlICJiYXIiCj4gIGVuZAo+IGVuZAo+Cj4gYmVnaW4KPiAgYSA9IEEubmV3Cj4gIGEu
Zm9vCj4gcmVzY3VlID0+IGUKPiAgcHV0cyBlLmJhY2t0cmFjZQo+IGVuZAo+Cj4gJCBydWJ5IGV4
Yy5yYgo+IGV4Yy5yYjozOmluIGBmb28nCj4gZXhjLnJiOjkKPgo+Cj4gLS0KPiBQb3pkcmF3aWFt
Cj4KPiBSYWRvc7NhdyBCdbNhdAo+IGh0dHA6Ly9yYWRhcmVrLmpvZ2dlci5wbCAtIG3zaiBibG9n
Cj4KPgoKCi0tIApHcmVnCmh0dHA6Ly9ibG9nLmdyZWduZXQub3JnLwo=
 
R

Rados³aw Bu³at

2009/1/11 Greg Hauptmann said:
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.

--=20
Pozdrawiam

Rados=B3aw Bu=B3at
http://radarek.jogger.pl - m=F3j blog
 
G

Greg Hauptmann

dGhhbmtzIC0gd29uZGVyIGlmIGdpdmVuIHlvdSBoYXZlIHRoZSBtZXRob2QgbmFtZSwgZmlsZW5h
bWUsIGxpbmUgbnVtYmVyLAp3aGV0aGVyIHRoZXJlIGlzIGEgd2F5IHRvIHdvcmsgb3V0IHRoZSBj
bGFzcyB5b3UncmUgaW4/ICBJZiBpdCBjb21lcyBkb3duIHRvCmhhdmluZyB0byBwYXJzZSB0aHJv
dWdoIHRoZSBmaWxlIGl0c2VsZiBpdCBtYXkgZ2V0IGEgYml0IG1lc3N5IGhvd2V2ZXIuLi4KCjIw
MDkvMS8xMSBSYWRvc7NhdyBCdbNhdCA8cmFkZWsuYnVsYXRAZ21haWwuY29tPgoKPiAyMDA5LzEv
MTEgR3JlZyBIYXVwdG1hbm4gPGdyZWcuaGF1cHRtYW5uLnJ1YnlAZ21haWwuY29tPjoKPiA+IHRo
YW5rcyAtIG5vIHdheSB0byBnZXQgdGhlIGNsYXNzIG5hbWUgJiBtZXRob2QgbmFtZSB0aGVuPwo+
ID4KPgo+IE1ldGhvZCBuYW1lIGlzIGluY2x1ZGVkIGluIGJhY2t0cmFjZS4gZS5iYWNrdHJhY2Ug
cmV0dXJucyBhcnJheSBvZgo+IHN0cmluZywgd2hlcmUgZWFjaCBzdHJpbmcgaGFzIGZvbGxvd2lu
ZyBmb3JtOgo+IGZpbGU6bGluZSBpbiBgbWV0aG9kJwo+IFlvdSBjYW4gdHJ5IHRvIHBhcnNlIGl0
IChzaW1wbHkgd2l0aCByZWdleHApLiBJJ20gc3VycHJpc2VkIHRoYXQgdGhlcmUKPiBpcyBubyBp
bmZvcm1hdGlvbiBhYm91dCBjbGFzcyBidXQgcHJvYmFibHkgdGhlcmUgaXMgcmVhc29uIGZvciB0
aGF0Lgo+Cj4gLS0KPiBQb3pkcmF3aWFtCj4KPiBSYWRvc7NhdyBCdbNhdAo+IGh0dHA6Ly9yYWRh
cmVrLmpvZ2dlci5wbCAtIG3zaiBibG9nCj4KPgoKCi0tIApHcmVnCmh0dHA6Ly9ibG9nLmdyZWdu
ZXQub3JnLwo=
 

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

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top