Exceptions

Ref. WGR Chapter 6, Control-flow techniques, Section 6.4, Error handling and exceptions

"exception" is a nice way of saying "error"

Some common exceptions

See? They're all errors! :-)

The Ruby Exception Hierarchy

Exception
 NoMemoryError
 ScriptError
   LoadError
   NotImplementedError
   SyntaxError
 SignalException
   Interrupt
 StandardError
   ArgumentError
   IOError
     EOFError
   IndexError
   LocalJumpError
   NameError
     NoMethodError
   RangeError
     FloatDomainError
   RegexpError
   RuntimeError
   SecurityError
   SystemCallError
   SystemStackError
   ThreadError
   TypeError
   ZeroDivisionError
 SystemExit
 fatal

plain rescue

begin
  x = 100 / y
rescue
  x = 0
end

type-specific rescue

x = begin
  100 / y
rescue ZeroDivisionError
  0
end

i want a raise

def even?(x)
  raise ArgumentError, "I need a number" unless x.is_a? Numeric
  x % 2 == 0
end

custom exception classes

class Whiz
  class NoJuice < StandardError
  end

  def bang
    unless power >= 1.21
      raise NoJuice, 
        "need at least 1.21 jigawatts, but only had #{power}"
    end
  end
end

multiple type-specific rescues

def divide_if_even(x, y)
  if even?(x)
    x/y
  end
rescue ArgumentError
  puts "hey! give me a number next time"
  1
rescue ZeroDivisionError
  0
end

rescueing the exception

x = begin
  100 / y
rescue ZeroDivisionError => e
  puts "oops! #{e.class}: #{e.message}"
  0
end

general rescue

    begin
      do_something_dangerous
    rescue => e
      puts e.class, e.message
    end

even more general rescue

begin
  do_something_really_dangerous
rescue Exception => e
  puts e.class, e.message
end

the full rescue song and dance

begin
  do_something_dangerous
rescue SomeError => e
  fix_some_error(e)
rescue SomeOtherError => e
  fix_some_other_error(e)
rescue => e
  fix_unknown_standard_error(e)
else
  something_dangerous_finished
ensure
  whether_or_not_an_error_was_raised
end

 Previous Lesson

Outline

[menu]

/