Upcoming Documentation for Exception Classes

NoMemoryError
Raised when memory allocation fails.
ScriptError
ScriptError is the superclass for errors raised when a script can not be executed because of a +LoadError+, +NotImplementedError+ or a +SyntaxError+. Note these type of +ScriptErrors+ are not +StandardExceptions+ and will not be rescued unless it is specified explicitly (or its ancestor +Exception+).
LoadError
Raised when a file required (a Ruby script, extension library, ...) fails to load.

require 'this/file/does/not/exist' # => LoadError: no such file to load -- this/file/does/not/exist
NotImplementedError
Raised when a feature is not implemented on the current platform. For example, methods depending on the +fsync+ or +fork+ system calls may raise this exception if the underlying operating system or Ruby runtime does not support them.
SyntaxError
Raised when encountering Ruby code with an invalid syntax.

eval("1+1=2") # => SyntaxError: (eval):1: syntax error, unexpected '=', expecting $end
SecurityError
Raised when attempting a potential unsafe operation (typically when the $SAFE level is raised above 0)

foo = "bar"
proc = Proc.new do
$SAFE = 4
foo.gsub! "a", "*"
end
proc.call # => SecurityError: Insecure: can't modify string
SignalException
Raised when a signal is received, for example by Process.kill

begin
Process.kill('HUP',Process.pid)
rescue SignalException => e
puts "received Exception #{e}"
end

==> received Exception SIGHUP

Interrupt
Raised with the interrupt signal is received, typically because the user pressed on Control-C (on most posix platforms). As such, it is a subclass of +SignalException+.

begin
puts "Press ctrl-C when you are bored"
loop {}
rescue Interrupt => e
puts "Note: You will typically use Signal.trap instead."
end
StandardError
The most standard error types are subclasses of StandardError. A rescue clause without an explicit Exception class will rescue all StandardErrors (and only those).

dosomethingthatraisesastandarderror rescue "Hello"

=> "Hello"

require 'does/not/exist' rescue "Hi"

=> LoadError: no such file to load -- does/not/exist

ArgumentError
Raised when the arguments are wrong and there isn't a more specific Exception class. For example, passing the wrong number of arguments or a value that is not acceptable will typically raise an ArgumentError.

[1, 2, 3].first(4, 5) # => ArgumentError: wrong number of arguments (2 for 1)
[1, 2, 3].first(-4) # => ArgumentError: negative array size
FiberError
Raised when an invalid operation is attempted on a Fiber, in particular when attempting to call/resume a dead fiber,attempting to yield from the root fiber, or calling a fiber across threads.

fiber = Fiber.new{}
fiber.resume # => nil
fiber.resume # => FiberError: dead fiber called
IndexError
Raised when the given index is invalid.

a = [:foo, :bar]
a.fetch(0) # => :foo
a[2] # => nil
a.fetch(2) # => IndexError: index 2 out of array
KeyError
Raised when the specified key is not found. It is a subclass of IndexError.

h = {:foo => :bar}
h.fetch(:foo) # => :bar
h.fetch(:baz) # => KeyError: key not found
StopIteration
Raised to stop the iteration, in particular by Enumerator#next. It is rescued by Kernel#loop.

loop do
puts "Hello"
raise StopIteration
puts "World"
end
puts "Done!"

=> Outputs "Hello" and "Done!"

IOError
Raised when an IO operation fails.

File.open("/etc/hosts") {|f| f << "example"} # => IOError: not opened for writing
File.open("/etc/hosts") {|f| f.close; f.read } # => IOError: closed stream

Note that some IO failures raise +SystemCallError+s and these are not subclasses of IOError:

File.open("does/not/exist") # => Errno::ENOENT: No such file or directory - does/not/exist
EOFError
Raised by some IO operations when reaching the end of file. Many IO methods exist in two forms,one that returns +nil+ when the end of file is reached, the other raises EOFError +EOFError+.+EOFError+ is a subclass of +IOError+.

file = File.open("/etc/hosts")
file.read
file.gets # => nil
file.readline # => EOFError: end of file reached
LocalJumpError
Raised when Ruby can't yield as requested.

def callblock
yield 42
end
call
block # => LocalJumpError: no block given (yield)

def getmeareturn
Proc.new { return 42 }
end
get
meareturn.call # => LocalJumpError: unexpected return
NameError
Raised when a given name is invalid or undefined.

puts foo # => NameError: undefined local variable or method `foo' for main:Object
Fixnum.constset :answer, 42 # => NameError: wrong constant name answer
NoMethodError
Raised when a method is called on a receiver which doesn't have it defined and also fails to respond with +method
missing+.

"hello".toary # => NoMethodError: undefined method `toary' for "hello":String
RangeError
Raised when a given numerical value is out of range.

[1, 2, 3].drop(1 << 100) # RangeError: bignum too big to convert into `long'
FloatDomainError
Raised when attempting to convert special float values (infinite or NaN) to numerical classes which don't support them.

Float::INFINITY.to_r # => FloatDomainError: Infinity
RegexpError
Raised when given an invalid regexp expression.

Regexp.new("?") # => RegexpError: target of repeat operator is not specified: /?/
RuntimeError
Raised when an invalid operation is attempted.

Kernel.raise will raise a RuntimeError if no Exception class is specified.

raise "ouch" # => RuntimeError: ouch
[1, 2, 3].freeze << 4 # => RuntimeError: can't modify frozen array
SystemCallError
SystemCallError is the base class for all low-level platform-dependent errors.The errors available on the current platform are subclasses of SystemCallError and are defined in the Errno module.

File.open("does/not/exist") # => Errno::ENOENT: No such file or directory - does/not/exist
ThreadError
Raised when an invalid operation is attempted on a thread.

Thread.stop # => ThreadError: stopping only thread
TypeError
Raised when encountering an object that is not of the expected type.

[1, 2, 3].first("two") # => TypeError: can't convert String into Integer
ZeroDivisionError
Raised when attempting to divide an integer by 0.

42 / 0 # => ZeroDivisionError: divided by 0
42 / 0.0 # => Float::INFINITY
SystemExit
Raised by exit to initiate the termination of the script.
SystemStackError
Raised in case of a stack overflow.

def memyselfandi
me
myselfandi
end
memyselfand_i # => SystemStackError: stack level too deep
Encoding::CompatibilityError
Raised by Encoding and String methods when the source encoding is incompatible with the target encoding.
Encoding::ConverterNotFoundError
Raised by transcoding methods when a named encoding does not correspond with a known converter.
Encoding::UndefinedConversionError
Raised by Encoding and String methods when a transcoding operation fails.
Encoding::InvalidByteSequenceError
Raised by Encoding and String methods when the string being transcoded contains a byte invalid for the either the source or target encoding.