Ruby StandardError & Exception
Author: Samuel Williams When: Wednesday, 05 August 2009April 2009
May 2009
August 2009
September 2009
October 2009
- Building a Concrete Bath
- LED Lighting Comparison
- Thinking about Programming Languages
- How To Be A Consultant
- Lucid Programming Dojo
- Exim4 + ClamAV + SpamAssassin
- Secure login using AJAX
- Ramaze And Rack
- ActiveMerchant
- Concurrency And Immutability
- Floating Point Numbers
- Programming And Debugging
- Useful jQuery Plugins
- Loading Anonymous Ruby Classes
- 尺八 (Shakuhachi)
- Card Trick
- Object Oriented C
- Gemcutter
- Writing Clearly
- Richard Stallman In Christchurch
- Magnatune
- Client Side Graphing
- Zena CMS
November 2009
February 2010
March 2010
April 2010
May 2010
June 2010
July 2010
August 2010
September 2010
December 2010
January 2011
March 2011
May 2011
August 2011
September 2011
It is important to understand the place of StandardError in the hierarchy of ruby exceptions:
class TestException1 < StandardError
end
class TestException2 < Exception
end
begin
begin
raise TestException1
rescue
puts $!.class.name
end
begin
raise TestException2
rescue
puts $!.class.name
end
rescue Exception
puts "Unhandled Exception #{$!.class.name}"
end
The result of this code is:
TestException1 Unhandled Exception TestException2
This behavior is consistent with the way ruby exceptions are designed; Errors which you can generally deal with are subclassed from StandardError, while errors which indicate a failure in the semantic meaning of the code or its execution inherit directly from Exception
class Object
def self.subclasses(direct = false)
classes = []
ObjectSpace.each_object(Class) do |c|
next unless c.superclass == self
classes << c
end
return classes
end
end
puts "Direct descendants of Exception: \n\t" + Exception.subclasses.join("\n\t")
puts "Direct descendants of StandardError: \n\t" + StandardError.subclasses.join("\n\t")
This gives us a list of exception classes:
# Direct descendants of Exception: NoMemoryError ScriptError StandardError SignalException fatal SystemExit # Direct descendants of StandardError: SystemStackError LocalJumpError IOError RegexpError ZeroDivisionError ThreadError SystemCallError SecurityError RuntimeError NameError RangeError IndexError ArgumentError TypeError
Generally, if you are making your own exception class for non-fatal execution errors, you should inherit from SystemError or one of its descendants.
Comments
Please note, you can leave a comment that uses (limited) XHTML and Textile syntax.