Ruby - Handler all possible exceptions

Introduction

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

Demo

def calc( val1, val2 )
   begin#  w ww.j a  v  a 2s  . com
       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