Sunday 29 April 2012

Exception Handling in ruby part 1(Exception Hierarchy of ruby)

 Exception Hierarchy

An exception is a special kind of object, an instance of the class Exception or a descendant of that class that represents some kind of exceptional condition; it indicates that something has gone wrong. When this occurs, an exception is raised (or thrown). By default, Ruby programs terminate when an exception occurs. But it is possible to declare exception handlers. An exception handler is a block of code that is executed if an exception occurs during the execution of some other block of code. Raising an exception means stopping normal execution of the program and transferring the flow-of-control to the exception handling code where you either deal with the problem that's been encountered or exit the program completely. Which of these happens - dealing with it or aborting the program - depends on whether you have provided a rescue clause (rescue is a fundamental part of the Ruby language). If you haven't provided such a clause, the program terminates; if you have, control flows to the rescue clause.
Ruby has some predefined classes - Exception and its children - that help you to handle errors that can occur in your program. The following figure shows the Ruby exception hierarchy.

       Exception
  1. Fatal(fatal)
  2. NoMemoryError
  3. ScriptError
    • LoadError
    • NotImplementedError
    • SyntaxError
  4. SignalException
    • Interrupt
  5. StandardError
    1. ArgumentError
    2. IOError
      • EOFError
    3. IndexError
    4. LocalJumpError
    5. NameError
      • NoMethodError
    6. RangeError
      • FloatDomainError
    7. RegexpError
    8. RuntimeError
    9. SecurityError
    10. SystemCallError
      • Errorno::__ (ENOENT, etc.) (system-dependent)
    11. ThreadError
    12. TypeError
    13. ZeroDivisionError
  6. SystemExit
  7. SystemStackError

1 comment:

  1. A good collection of hierarchy. and nice explanication in next post

    ReplyDelete