Ruby - Exception Handler Syntax

Introduction

rescue's syntax makes handling different exceptions in different ways easy:

begin 
  ... code here ... 
rescue ZeroDivisionError 
  ... code to rescue the zero division exception here ... 
rescue YourOwnException 
  ... code to rescue a different type of exception here ... 
rescue 
  ... code that rescues all other types of exception here ... 
end 

This code contains multiple rescue blocks, each of which is caused depending on the type of exception raised.

If a ZeroDivisionError is raised within the code between begin and the rescue blocks, the rescue ZeroDivisionError code is executed to handle the exception.

Related Topic