For those like me who are entering the ruby world but have programming experience with other languages, it can be hard to get things done, but that’s a part of ruby, doing things differently
On a language like Java or C# the syntax is:
Upgrade your computer with an SSD
try { some_code(); } catch(Exeption) { handle_expeption(); } finally { this_code_is_always_executed(); }
On Ruby the ideia is the same, but the syntax is a little different:
begin – rescue – ensure
Lets see:
begin some_code rescue handle_error ensure this_code_is_always_executed end
If you need to log the error, you can have access to the error message inside rescue block:
@error_message="#{$!}"
Hope you find it as useful as I did
its working thanks
if i want to catch or pick the element which got failed, in a report. In java we to to catch using printstacktrace n all type methods. Do we have anything in ruby to do this. I want to catch the element which got failed while running my ruby script.
begin
your_code
rescue => err
logger.error err.message
end