Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to start the Ruby debugger on exception?

Is there any way to start the/a Ruby debugger whenever the code throws an exception, without me wrapping the code like this:

begin
  #do something
rescue
  debugger
end

I'd like to have it in such a way that if the do something part raises an exception, the debugger will start. It would be nice not having to modify the code to add begin rescue blocks all over.

like image 894
Geo Avatar asked Feb 24 '10 15:02

Geo


4 Answers

Hammertime!

like image 60
Andrew Grimm Avatar answered Oct 23 '22 13:10

Andrew Grimm


I stumbled across this page:post-mortem debugging. Doing this:

Debugger.start(:post_mortem => true)

gets me where I want to.

like image 43
Geo Avatar answered Oct 23 '22 14:10

Geo


require 'ruby-debug'
class Exception
  alias original_initalize initialize
  def initialize(*args)
    original_initalize(*args)
    debugger
  end
end

This will run the original exception as well as call debugger

like image 3
Unixmonkey Avatar answered Oct 23 '22 13:10

Unixmonkey


In RubyMine 2.0.x go to Run -> View Breakpoints and click "Ruby Exception Breakpoints" tab, then add the type of the exception you are interested in...

There should be something similar in NetBeans and other Ruby IDEs i guess.

BTW, RubyMine is the BEST!

like image 2
Alex Kovshovik Avatar answered Oct 23 '22 15:10

Alex Kovshovik