Home > Ruby > Ruby’s try{ catch } block

Ruby’s try{ catch } block

October 16th, 2007 tony Leave a comment Go to comments

As are many posts on this blog, this one is just a reminder for myself and anyone else who can’t remember the ruby equivalent of a try/catch block and has had to Google it several times because their memory sucks:

begin

rescue

end

Now next time I Google it I won’t have to sift through 10 articles. :)

Categories: Ruby Tags:
  1. October 20th, 2007 at 04:36 | #1

    being

    rescue

    else

    ensure

    end

    Something else to google about :)

  2. guy inny
    December 28th, 2007 at 11:58 | #2

    I want to meet the person in ruby who came up with this idea of naming them as begin.. rescue… end.
    Thats one frantic whose siter would make it to every ruby programmers most dirty dreams.

  3. Geoff
    June 9th, 2008 at 15:52 | #3

    Hey an important missing piece is that you want:

    begin
    # something potentially bad
    rescue Exception=>e
    # handle e
    end

    I have done this with a require that wasn’t available and it
    wouldn’t work without the Exception=>e

    Of course Exception should be fine tuned to the Exception
    you are checking for.

  4. December 6th, 2008 at 20:07 | #4

    The Ruby bundle for TextMate has a nice shortcut for this:
    begin ⇥ (begin + [tab])

  5. bobby
    February 9th, 2009 at 23:37 | #5

    Thanks! Works like a charm, it was exactly what I was looking for, haha!