Ruby - Handler all possible exceptions

Introduction

Exceptions are objects, and they are defined by a class.

Demo

def calc( val1, val2 )
   begin#   w  w  w.java 2 s .  c om
       result = val1 / val2
   rescue Exception => e
       puts( e.class )
       puts( e )
       result = nil
   end
   return result
end

calc( 20, 0 )
calc( 20, "100" )
calc( "100", 100 )

Result

Related Topic