Skip to content

Instantly share code, notes, and snippets.

@arnab
Created November 19, 2010 00:44
Show Gist options
  • Save arnab/705964 to your computer and use it in GitHub Desktop.
Save arnab/705964 to your computer and use it in GitHub Desktop.
Standard Exceptions in Ruby (MRI 1.9 and 1.8) and how to generate them
ruby-1.8.7-p302 > tree_printer.call tree
Exception
IRB::Abort
NoMemoryError
ScriptError
LoadError
NotImplementedError
SyntaxError
SignalException
Interrupt
StandardError
ArgumentError
Exception2MessageMapper::ErrNotRegisteredException
IOError
EOFError
IRB::CantChangeBinding
IRB::CantReturnToNormalMode
IRB::CantShiftToMultiIrbMode
IRB::IllegalParameter
IRB::IrbAlreadyDead
IRB::IrbSwitchedToCurrentThread
IRB::NoSuchJob
IRB::NotImplementedError
IRB::Notifier::ErrUndefinedNotifier
IRB::Notifier::ErrUnrecognizedLevel
IRB::SLex::ErrNodeAlreadyExists
IRB::SLex::ErrNodeNothing
IRB::UndefinedPromptMode
IRB::UnrecognizedSwitch
IndexError
StopIteration
LocalJumpError
NameError
NoMethodError
RangeError
FloatDomainError
RegexpError
RubyLex::AlreadyDefinedToken
RubyLex::SyntaxError
RubyLex::TerminateLineInput
RubyLex::TkReading2TokenDuplicateError
RubyLex::TkReading2TokenNoKey
RubyLex::TkSymbol2TokenNoKey
RuntimeError
SecurityError
SystemCallError
SystemStackError
ThreadError
TypeError
ZeroDivisionError
SystemExit
fatal
=> [Exception]
ruby-1.9.2-p0 > tree_printer.call tree
BasicObject
Exception
IRB::Abort
NoMemoryError
ScriptError
LoadError
NotImplementedError
SyntaxError
SecurityError
SignalException
Interrupt
StandardError
ArgumentError
EncodingError
Encoding::CompatibilityError
Encoding::ConverterNotFoundError
Encoding::InvalidByteSequenceError
Encoding::UndefinedConversionError
Exception2MessageMapper::ErrNotRegisteredException
FiberError
IOError
EOFError
IRB::CantChangeBinding
IRB::CantReturnToNormalMode
IRB::CantShiftToMultiIrbMode
IRB::IllegalParameter
IRB::IrbAlreadyDead
IRB::IrbSwitchedToCurrentThread
IRB::NoSuchJob
IRB::NotImplementedError
IRB::Notifier::ErrUndefinedNotifier
IRB::Notifier::ErrUnrecognizedLevel
IRB::SLex::ErrNodeAlreadyExists
IRB::SLex::ErrNodeNothing
IRB::UndefinedPromptMode
IRB::UnrecognizedSwitch
IndexError
KeyError
StopIteration
LocalJumpError
Math::DomainError
NameError
NoMethodError
RangeError
FloatDomainError
RegexpError
RubyLex::AlreadyDefinedToken
RubyLex::SyntaxError
RubyLex::TerminateLineInput
RubyLex::TkReading2TokenDuplicateError
RubyLex::TkReading2TokenNoKey
RubyLex::TkSymbol2TokenNoKey
RuntimeError
SystemCallError
ThreadError
TypeError
ZeroDivisionError
SystemExit
SystemStackError
fatal
=> [BasicObject]
exceptions = []
tree = {}
ObjectSpace.each_object(Class) do |cls|
next unless cls.ancestors.include? Exception
next if exceptions.include? cls
next if cls.superclass == SystemCallError # avoid dumping Errno's
exceptions << cls
cls.ancestors.delete_if {|e| [Object, Kernel].include? e }.reverse.inject(tree) {|memo,cls| memo[cls] ||= {}}
end
indent = 0
tree_printer = Proc.new do |t|
t.keys.sort { |c1,c2| c1.name <=> c2.name }.each do |k|
space = (' ' * indent); space ||= ''
puts space + k.to_s
indent += 2; tree_printer.call t[k]; indent -= 2
end
end
tree_printer.call tree
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment