Exceptions

Rescue ... else ... ensure

  • Handling exceptions
  • Performing clean-up
begin
statements
rescue <class>
statements
[retry]
else
statements
ensure
statements
end
  • The rescue clause is executed if a matching exception was raised
  • The else clause is only executed if no exceptions are raised
  • The ensure clause is always executed when the block terminates (even if an exception was raised)
  • A retry statement within a rescue clause will repeat the entire block

Raise

  • Raising exceptions
raise                              # Reraise the current exception
raise "text" # Raise a RuntimeError exception
raise CLASS, "text" [, caller]
raise OBJECT, "text" [, caller]

raise CLASS, "text", caller[m..n]

Catch ... throw

  • throw causes the stack to be unwound until the catch block is reached
catch (:LABEL) do
...
throw :LABEL
end