Ruby’s try{ catch } block
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
being
…
rescue
…
else
…
ensure
…
end
Something else to google about
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.
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.
The Ruby bundle for TextMate has a nice shortcut for this:
begin ⇥ (begin + [tab])
Thanks! Works like a charm, it was exactly what I was looking for, haha!