Ruby - Omitting begin and end in exception handler

Introduction

When handling exceptions, the begin keyword may be omitted.

You can optionally omit begin and end when trapping exceptions inside a method, a class, or a module.

For example, all the following are legal:

def calc
        result = 1/0
    rescue Exception => e
        puts( e.class )
        puts( e )
        result = nil
    return result
end

class X
        @@x = 1/0
    rescue Exception => e
        puts( e.class )
        puts( e )
end

module Y
        @@x = 1/0
    rescue Exception => e
        puts( e.class )
        puts( e )
end

Related Topic