Use a modified begin/rescue

Hi,

I'm not exactly sure what you mean. If you only want to change how
errors are displayed, why not simply wrap the whole script in a
begin-end block?

Single begin-end blocks are only necessary if you actually want to do
something specific with the error. It doesn't make sense to write the
same error handler for every code that may faild.

However, there are ways to shorten error code. For single statements,
you can use the rescue modifier:

"" + 1 rescue puts 'Rescued!'

This catches any StandardError raised by the statement before the
"rescue".

You may also write a method that takes a block and catches errors from
it:

def try_and_catch
  begin
    yield
  rescue
    puts 'Rescued!'
  end
end

try_and_catch do
  "" + 1
end

···

--
Posted via http://www.ruby-forum.com/.