Ruby's Exception Hierarchy

Posted by Nick Sieger

Tim Bray:

Today I needed to know the class hierarchy under Exception, and maybe it’s there online but I couldn’t find it. Blecch. Hint: Pickaxe, 2nd ed., page 462.

Well, you could always use Ruby itself, too, that way you’ll always have an up-to-date list:

exceptions = [] tree = {} ObjectSpace . each_object ( Class ) do | cls | next unless cls . ancestors . include? Exception next if exceptions . include? cls next if cls . superclass == SystemCallError 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

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