Ref. WGR Chapter 6, Control-flow techniques, Section 6.4, Error handling and exceptions
RuntimeError
NoMethodError
IOError
ArgumentError
See? They're all errors! :-)
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
rescue
begin
x = 100 / y
rescue
x = 0
end
y
is 0, then it will raise a ZeroDivisionError
rescue
block will save us and make x
be 0 insteadrescue
x = begin
100 / y
rescue ZeroDivisionError
0
end
begin
raise
def even?(x)
raise ArgumentError, "I need a number" unless x.is_a? Numeric
x % 2 == 0
end
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
rescue
sdef 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
rescue
ing the exceptionx = begin
100 / y
rescue ZeroDivisionError => e
puts "oops! #{e.class}: #{e.message}"
0
end
rescue
begin
do_something_dangerous
rescue => e
puts e.class, e.message
end
rescue
begin
do_something_really_dangerous
rescue Exception => e
puts e.class, e.message
end
rescue => e
only catches StandardError
subclassesNoMemoryError
s really should stop your program dead unless you know betterrescue
song and dancebegin
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
/