<p>Consider the following ruby code</p>

<p>test.rb:</p>

<pre><code>begin

  puts
  thisFunctionDoesNotExist
  x = 1+1
rescue Exception =&gt; e
  p e
end
</code></pre>

<p>For debugging purposes, I would like the rescue block to know that the error occurred in line 4 of this file.  Is there a clean way of doing that?</p>

```
```
p e.backtrace
```
```
I ran it on an IRB session which has no source and it still gave relevant info.
```
```
=> ["(irb):11:in `foo'", 
    "(irb):17:in `irb_binding'", 
     "/usr/lib64/ruby/1.8/irb/workspace.rb:52:in `irb_binding'", 
     "/usr/lib64/ruby/1.8/irb/workspace.rb:52"]
```
```
If you want a nicely parsed backtrace, the following regex might be handy:
```
```
p x.backtrace.map{ |x|   
     x.match(/^(.+?):(\d+)(|:in `(.+)')$/); 
    [$1,$2,$4] 
}

[
  ["(irb)", "11", "foo"], 
  ["(irb)", "48", "irb_binding"], 
  ["/usr/lib64/ruby/1.8/irb/workspace.rb", "52", "irb_binding"], 
  ["/usr/lib64/ruby/1.8/irb/workspace.rb", "52", nil]
]
```
```
( Regex /should/ be safe against weird characters in function names or directories/filenames ) 
( If you're wondering where foo camefrom, i made a def to grab the exception out :
```
```
>>def foo
>>  thisFunctionDoesNotExist
>> rescue Exception => e 
>>   return e 
>>end     
>>x = foo 
>>x.backtrace
```
```