CSC/ECE 517 Fall 2009/wiki1b 3 NCS

From Expertiza_Wiki
Revision as of 13:45, 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 track the stack backwards to find an exception handler that can handle the raised exception.


Provide infrastructure to communicate exceptions from the methods where it occurs to the exception handlers. Provide a mechanism to pass objects from the exception raiser to handler.


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


We will cover exception handling goals and characteristics for Object Oriented languages in general and will go over a concrete example for exception handling in JAVA language.

A typical exception handling flow in most programming languages flows something like When an error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception object, contains information about the error, including its type and the state of the program when and where the error occurred. Creating an exception object and handing it to the runtime system is called throwing an exception.

Most Object Oriented languages with the exception of Smalltalk* have exception handling systems that provide constructs to the programmer to

- Raising an exception. Most programming languages provide a statement (throw/raise) to initiate an exception.

- Exception handlers Most Object Oriented languages provide a means to write exception handling blocks (catch/rescue/except) and also a means to specify the scope of the handlers (try block)

- Match Exceptions with exceptions handlers Provide a stack of handlers that can be traversed to match an exception with an appropriate handler

- Exception Objects Provide infrastructure to communicate exceptions from the methods where it occurs to the exception handlers. Provide a mechanism to pass objects from the exception raiser to handler.

- Exception handler options - Provide the handler means to either resume/retry/terminate the program or propagate the same or a new exception. Possibly spawn a debug process

- Allows exceptions to be organized in an inheritance hierarchy allowing sharing common behaviors and actions for a set of exceptions.

- Allow trapping multiple exceptions with a single handler.


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