February 2, 2004 Intro to Ruby Slide #29
Prev Next

Exceptions

     def foo
        raise StandardError, "I like Ruby" if rand < 0.5
     end

     begin
       foo  # exception might get raised here

       rescue StandardError
         print "StandardError was raised: $!\n"

       rescue
         print "Some other exception was raised: $!\n"

       else
         print "No exception was raised\n"

       ensure
         print "This code gets called no matter what\n"

     end


Prev Copyright © 2004 Walter C. Mankowski Next