Skip to content

Instantly share code, notes, and snippets.

@diegodurs
Created November 22, 2013 10:42
Show Gist options
  • Save diegodurs/7597967 to your computer and use it in GitHub Desktop.
Save diegodurs/7597967 to your computer and use it in GitHub Desktop.
Exceptions
# 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