Ruby - Exception Handler Syntax


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

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

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