CSC/ECE 517 Fall 2009/wiki1b 3 NCS

From Expertiza_Wiki
Revision as of 14:18, 20 September 2009 by NCS (talk | contribs)
Jump to navigation Jump to search

Exception handling in Object Oriented Languages

An exception is an abnormal event, which occurs during the execution of a program that interrupts the normal flow of the program. Exception handling is the programming constructs a language provides to handle exceptions.

Programming Constructs for exception handling

Exception handling systems in general strive to improve software reliability, readability, usability, maintainability and easier debugging.

Most modern Object Oriented Language exception handling systems provide the programmers a means to

Raising an exception

When an error occurs within a method, the method creates an object and hands it off to the runtime system. Creating an exception object and handing it to the run time system is called throwing an exception. This done typically using either a throw or raise statement. This statement takes the exception object as an argument.

Create Exception Objects

The Exception Object typically contains the following information about the error

  • Type of error
  • State of the program
  • Where the error occurred

Object Oriented languages also allow the programmer to organize the exceptions objects in an inheritance hierarchy allowing the sharing of common behaviors and actions for a set of exceptions.

Write Exception handlers

Exception handlers are generally written as blocks of code ( catch/rescue/except blocks ) that can specify the types of exceptions they can handle along with a means to specify the scope of the handler ( try block ). Multiple exceptions can be caught with a single handler.

Using the above statements a programmer can specify a stack of handlers that can be traversed to match an exception with an appropriate handler

Match Exceptions with exceptions handlers

Once an exception is raised the run time system will traverse the stack backwards to find an exception handler that can handle the raised exception. Once a handler is found that can handle the raised exception the run time system passes control to the handler along with the exception object.

Handling Exceptions

The exception handler routine can look at the exception caught and may decide to perform one of the following actions

  • Resume the program or retry the failed operation
  • Propagate the exception
  • Terminate the program
  • Spawn a debug process


Example: Exception handling in JAVA

Specifying Exception handlers Try catch and finally blocks

The first step for writing exception handlers is to specify the scope of the handler using a try block. the try block encloses the code that can throw exceptions that the exception handler plans on handling. The exception handler itself is specified within the catch block. Each catch block handles the type of exception as specified by the argument of the catch block. Finally the finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. The finally block also serves the purpose of avoiding bypassing the clean up code accidentally in the case of an exception or accidentally by a return or a break statement.

 try {

// code

   } catch (ArrayIndexOutOfBoundsException e) {

// handler for ArrayIndexOutOfBoundsException

   } catch (IOException e) {

//handler for IO exception

   } finally {

// clean up

   }

Throwing an Exception The throw statement is used to throw an exception. The throw statement requires a single argument: a throwable object. (subclass of the Throwable class). Here's an example of a throw statement.

   throw someThrowableObject;

Specifying exceptions thrown by an Method

public void foo() throws IOException,

                              ArrayIndexOutOfBoundsException {

}


Types of exceptions All exceptions that can be thrown are subclassed from Throwable class

Object | Throwable | __________________________________

| | Error Exception ... | ___________________ | | ...... RunTimeException .....


Errors are hard errors within the JVM. Applications typically do not handle or throw Errors

Exceptions - Checked vs Unchecked.

Checked exceptions - all exceptions that must be specified and caught Unchecked exceptions - subclasses of RunTimeException calss these do need to be specified or caught


  • Small talk allows exception handlers to be only associated with classes and when an exception is raised it searches for the handler in the class where the exception was raised.

http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html