Name
begin Statement
Synopsis
begincode
[rescue [exception_class
[,exception_class
...]] [=>variable
] [then]code
]... [elsecode
] [ensurecode
] end
The begin
statement
encloses code
and performs exception
handling when used together with the rescue
and
ensure
clauses.
When a rescue
clause
is specified, exceptions belonging to the
exception_class
specified are caught, and
the code
is executed. The value of the
whole begin
enclosure is the value of its last
line of code
. If no
exception_class
is specified, the program
is treated as if the StandardError
class had been
specified. If a variable
is specified, the
exception object is stored to it. The rescue
exception_class
is separated from the rest
of the code by the reserved word then
, a newline,
or a semicolon. If no exceptions are raised, the
else
clause is executed if specified. If an
ensure
clause is specified, its
code
is always executed before the
begin
/end
block exits, even if
for some reason the block is exited before it can be completed.
Get Ruby in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.