# basic exception handling begin nil + 1 rescue Exception => e $stderr.puts( e ) end # defining your own exceptions class MyException < Exception def initialize( msg ) super( msg ) end end # throwing your own exception begin raise MyException.new( "Error msg!" ) rescue MyException => e $stderr.puts( e ) end # java's finally block is also in ruby, but it is known as ensure file = open( "file" ) begin file.each do |line| print line end rescue Exception => e $stderr.puts( e ) ensure file.close end