Tuesday, October 28, 2008

Ruby on Rails - try catch finally block equivalent - exception catching

Looking for Ruby equivalent for "try catch finally"?

I also have a problem with that from time to time ;)
Its "begin rescue ensure end"
begin
somecode()
rescue

puts "Error #{$!}"
ensure

this_code_will_execute_always()
end

18 comments:

  1. why did they change these names? Thanx for that.

    ReplyDelete
  2. it's important not to confuse the fact that ruby has a throw/catch AND a begin/raise/rescue/finally construct; the former for jumping out of a call stack, the latter for doing the same but during error conditions. While posts like this may be helpful for someone coming from a java or php background, they make it difficult to search for the actual throw/catch constructs in ruby

    ReplyDelete
  3. thanks! very helpful!

    ReplyDelete
  4. Thanks you! I needed this.

    ReplyDelete
  5. This comment has been removed by a blog administrator.

    ReplyDelete
  6. Awesome post presented by you..your writing style is fabulous and keep update with your blogs Ruby on Rails Online Course Bangalore

    ReplyDelete