Created
November 22, 2013 10:42
-
-
Save diegodurs/7597967 to your computer and use it in GitHub Desktop.
Exceptions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# credits goes to SKORKS | |
# http://www.skorks.com/2009/09/ruby-exceptions-and-exception-handling/ | |
# raise an exception | |
raise TypeError, 'You must give me truth' if value == false | |
# raise RuntimeError | |
raise "Hello" | |
# basic block | |
begin | |
raise "foo" | |
rescue StandardError => e # or rescue => e | |
puts e.message | |
puts e.backtrace | |
ensure | |
puts "Hello World" | |
end | |
# create your own | |
class MyCrazyException < StandardError | |
end | |
raise MyCrazyException, "I am a crazy new exception" | |
# Ruby 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 | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment