CSC/ECE 517 Fall 2009/wiki1b 3 NCS

From Expertiza_Wiki
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

Exception hierarchy

All Exception objects in JAVA are sub classed from the Exception Object.
http://www.artima.com/designtechniques/images/exceptFig1.gif


  • Checked vs Unchecked Exceptions

Checked exceptions must be explicitly caught or propagated as described in Basic try-catch-finally Exception Handling. Unchecked exceptions do not have this requirement. They don't have to be caught or declared thrown. Unchecked exceptions extend the java.lang.RuntimeException. Checked vs Unchecked the Controversy.

  • Errors

Errors are hard errors within the JVM. Applications typically do not handle or throw Errors. Errors are unchecked exceptions. Example NullPointerException.

Throwing an Exception

The throw statement is used to throw an exception. The throw statement requires a single argument: a throwable object. Here's an example of a throw statement.

   throw someThrowableObject;

Specifying exceptions thrown by an Method

   public void foo() throws IOException, 
         ArrayIndexOutOfBoundsException {
   }

Propagating an exception

You don't have to catch exceptions thrown from other methods. If you cannot do anything about the exception where the method throwing it is called, you can just let the method propagate the exception up the call stack to the method that called this method. If you do so the method calling the method that throws the exception must also declare to throw the exception.

You can also catch the exception and throw a new exception that the calling layer can understand and recover from.

 void updateNameInDBFile(String Name) throws IOException {
    % Performs some file IO operation and propagates the IOException to calling function
 }
 ...
 try {
    % code calling updateNameInDBFile
 } catch (IOException e) {
   throw new NameNotUpdatedException(e); // maps IOException to NameNotUpdatedException
 }

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 that might throw an exception
 }
 catch (SomeException se) 
 { 
    % code that handles this exception
 }
 catch (SomeOtherException soe)
 {  
    % code that handles this exception
 }
 finally   % optional block
 {
    % This code will always get executed
 }


References

[1] Exception handling in Object Oriented Systems

[2]Sun Java Tutorial