Use a modified begin/rescue


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

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

def try_and_catch
    puts 'Rescued!'

try_and_catch do
  "" + 1


Posted via