Search code examples
rubyexceptionerror-handlingrescue

How does one use rescue in Ruby without the begin and end block


I know of the standard technique of having a begin <some code> rescue <rescue code> end

How does one just use the rescue block on its own?

How does it work, and how does it know which code is being monitored?


Solution

  • A method "def" can serve as a "begin" statement:

    def foo
      ...
    rescue
      ...
    end