Ruby Exceptions
Click here to load reader
-
Upload
guest0046de -
Category
Technology
-
view
508 -
download
0
Transcript of Ruby Exceptions
‘Exception Handling in RUBY’
‘Exception Handling in RUBY’
Joy Menon
Department of Computer Science and Engineering, IIT Mumbai.
24 November 2004
‘Exception Handling in RUBY’
A Brief Outline
I Exception Handling: WHY?I Errors and Error CodesI The Need for Exception Handling
I Exception Handling in Ruby:I The Exception ClassI Exception Class HeirarchyI Handling ExceptionsI Raising ExceptionsI Catch/Throw Clauses
I Conclusions
‘Exception Handling in RUBY’
Exception Handling: WHY?
Errors and Error Codes
Errors and Error Codes
I In any programming, occurance of errors is a distinct reality.
I Errors need to be handled gracefully to avoid abrupt failures.I Code that detects error may not have context to handle it.
I For example, attempting to open a file that doesn’t exist isacceptable in some circumstances and is a fatal error at othertimes. What does the file-handling module do?
I Conventionally it was done using error-checking andreturn-codes mechanism.
I Functions were checked for return values, and if the returncode indicated failure, this code was interpreted and passedup the call stack.
‘Exception Handling in RUBY’
Exception Handling: WHY?
The Need for Exception Handling
The Need for Exception Handling
I However Error-codes mechanism has following shortcomings:I Handling all errors through error codes is simply not possible.I Moving error codes up the function call stack is complicated.I Managing all the error-codes and associated code is tedious.
I The Exception Handling mechanism addresses theseshortcomings.
I Exceptions allow packaging info about error in an object.I Exception handling helps propagate object up the call stack.I Runtime system locates code that knows to handle the error.
I Exception handling is essential for achieving well-designed object
oriented code, and therefore Ruby provides a mechanism for same.
‘Exception Handling in RUBY’
Exception Handling in Ruby
The Exception Class
The Exception Class
I As in other object oriented languages, Ruby offers amechanism for exception handling.
I When an exception occurs..I Object of class Exception, or one of it’s children, created.I Exception is associated to message string & a stack backtrace.I All information about the exception is packaged in this object.
I IOError, ZeroDivisionError, TypeError, SystemCallError, etcare examples of exceptions derived from class Exception.
I Ruby predefines a hierarchy of exceptions: see next slide.
‘Exception Handling in RUBY’
Exception Class Heirarchy
Exception Class Heirarchy
Figure: Ruby: The Exception Class Hirarchy
‘Exception Handling in RUBY’
User Defined Exceptions
User Defined Exceptions
User-Defined Exception
I Users can create exception classes of their own.
I These must inherit from class StandardError or its children.
I If they dont, such exceptions will not be detected by default.
I They may include more specific information about theexception.
‘Exception Handling in RUBY’
User Defined Exceptions
Handling Exceptions
Handling Exceptions
I The basic approach to exception handling involves the use of:I Enclose candidate code in begin/end block.I Use rescue block to handle specific class of exceptions,
where:I Report the error,I Code to handle detected error,I Raise the exception using raise.
I Use ensure block to ensure execution of some essential codeafter handling, like deallocation of resources such as DBconnections, etc.
I We can draw analogies to C++/Java exception handling:I begin/end block for candidate code. (like try block)I rescue blocks for handling code. (like catch blocks)I raise command for raising the exception. (like throw)I ensure command for necessary handling. (like final)
‘Exception Handling in RUBY’
User Defined Exceptions
Handling Exceptions
Example: Using rescue, raise and ensure
‘Exception Handling in RUBY’
User Defined Exceptions
Handling Exceptions
Handling Exceptions (continued..)
I VariablesI $ refers to the default global variable that stores the exception
object for the exception.I stderrobj is an example of user-defined variable storing the
reference to the exception object.
I Matching exceptions to correct rescue block is like the casestatement mechanism,
I object.kindof? result compared to exception types inrescue statements.
‘Exception Handling in RUBY’
User Defined Exceptions
Raising Exceptions
Raising Exceptions
I Raising exceptions deliberately in code help to trigger alertswhere errors are expected and need to be handled.
I The raise statement can take the following forms:I raise
Raises current exception again, or RuntimeError if none.
I raise <message>Creates new RuntimeError exception, associates it withmentioned string, and raises the exception up the call stack.
I raise <exception class>, <message>, <call stack>Creates exception with arg-1 used as required exception type,arg-2 as message string, and arg-3 as stack trace.
‘Exception Handling in RUBY’
User Defined Exceptions
Raising Exceptions
Example: Typical Usage of raise
‘Exception Handling in RUBY’
User Defined Exceptions
The Catch/Throw Clauses
The catch/throw Clauses
I The rescue and raise mechanism is thus used forabandoning execution when unwanted errors occur.
I The catch and throw mechanism is used to continueexecution at some point up the call stack.
I Working of catch/throw:I catch defines a block with a specific label.I The block is exeuted, typically accross nested functions, till a
throw followed by the label is encountered.I The call stack is then scoured for a catch block with a
matching label.I On a match, Ruby rolls back execution to the point and
terminates the block.I If the throw had an optional 2nd argument, it is returned as
value of the catch block.
‘Exception Handling in RUBY’
User Defined Exceptions
The Catch/Throw Clauses
Example: Typical usage of catch/throw
‘Exception Handling in RUBY’
Conclusions
Conclusions
I Exception Handling is essential for managing errors inobject-oriented code.
I The Ruby Exception Handling mechanism includes:I rescue and final clauses - for handling unwanted errors and
exit gracefully.
I raise - for deliberately creating and raising exceptions incode.
I catch and throw clause - for continuing execution at somepoint up the function call stack.
‘Exception Handling in RUBY’
Conclusions
References
http://www.insula.cz/dali/material/rbycl/.
http://www.ruby-lang.org/.
Dave Thomas and Chad Fowler and Andy Hunt.Programming Ruby: The Pragmatic Programmers’ Guide.Pragmatic Bookshelf, Oct 2004.