CSC/ECE 517 Fall 2009/wiki1b 3 NCS: Difference between revisions
Line 4: | Line 4: | ||
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. | 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. | ||
== Goals for a well designed exception handling system | |||
*Improves software reliability | |||
*readability | |||
*usability | |||
*easier debugging and maintainability. | |||
A typical exception handling flow in most programming languages flows something like | A typical exception handling flow in most programming languages flows something like |
Revision as of 13:02, 20 September 2009
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.
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. == Goals for a well designed exception handling system
- Improves software reliability
- readability
- usability
- easier debugging and maintainability.
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