Exceptions Don’t Frustrate Your User – Handle Errors

17
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net

description

Exceptions Don’t Frustrate Your User – Handle Errors. KR – CS 1401 Spring 2005 Picture – sysprog.net. Exceptions Writing Reliable Code. We want to write applications that are reliable. Reliability depends upon correctness and robustness : - PowerPoint PPT Presentation

Transcript of Exceptions Don’t Frustrate Your User – Handle Errors

Page 1: Exceptions  Don’t Frustrate Your User – Handle Errors

Exceptions Don’t Frustrate Your User – Handle Errors

KR – CS 1401 Spring 2005

Picture – sysprog.net

Page 2: Exceptions  Don’t Frustrate Your User – Handle Errors

Exceptions Writing Reliable Code

We want to write applications that are reliable.

Reliability depends upon correctness and robustness:• Correctness - the program produces correct results for

valid input.• Robustness – the program can handle error conditions

without crashing too easily.

Robustness can be increased by planning for and handling unusual circumstances, called exceptions.

KR – CS 1401 Spring 2005

Page 3: Exceptions  Don’t Frustrate Your User – Handle Errors

Exceptions Possible sources of Error

An exception is an unusual error condition that occurs while the program is running.

What are some sources of these errors ?

1. System or network-related I/O problems:

- A file may become corrupted or removed.

- Network congestion or an overloaded server may prevent access to a file.

- The server may be down.

2. User input error, such as entering a string that cannot be converted into a number as expected by application.

KR – CS 1401 Spring 2005

Page 4: Exceptions  Don’t Frustrate Your User – Handle Errors

Exceptions

When an exception occurs, the system is alerted and an exception is said to be thrown.

When an exception is thrown, the normal sequence of code execution is halted, and the exception is caught by appropriate exception-handling routines.

KR – CS 1401 Spring 2005

Page 5: Exceptions  Don’t Frustrate Your User – Handle Errors

ExceptionsClasses of Exception Objects

Exceptions in Java are defined as objects of various exception classes.

Exception classes form an inheritance hierarchy.

All exceptions are subclasses of the class Throwable.

You may throw any of the predefined exceptions to handle an error condition, or you may define and throw your own exception subclass to handle errors in a specific way appropriate for your application.

KR – CS 1401 Spring 2005

Page 6: Exceptions  Don’t Frustrate Your User – Handle Errors

ExceptionsInheritance Diagram of Exception Classes

KR – CS 1401 Spring 2005

Diagram – Sun Microsystems

Page 7: Exceptions  Don’t Frustrate Your User – Handle Errors

Exceptions How is an exception handled?

If an exception is thrown within a method, the JVM looks for code within the local method to catch (handle) it.

If there is none, the method returns and the JVM looks in the calling method for code to catch the exception.

This can cause a chain of returns all the way back to the JVM. At that point, the program terminates and a stack trace is displayed.

Up to now we have thrown exceptions to the JVM to catch:public static void main (String [] args) throws IOException {…}

KR – CS 1401 Spring 2005

Page 8: Exceptions  Don’t Frustrate Your User – Handle Errors

Exceptions Exception handling

KR – CS 1401 Spring 2005

Diagrams – Sun Microsystems

Page 9: Exceptions  Don’t Frustrate Your User – Handle Errors

Exceptions Writing our own code

We can write our own code to throw and catch exceptions.

The structure to use is a try-catch-finally statement:• Block of code that may result in an error and thrown

exception (try).• One or more blocks of code to handle exception(s)

thrown (catch). • Optional block of code performing “clean-up” or

other statements to be executed regardless of whether there is an error or not (finally). Ex: closing a file.

KR – CS 1401 Spring 2005

Page 10: Exceptions  Don’t Frustrate Your User – Handle Errors

Exceptions Syntax of try-catch-finally statement

try {

statement

statement …

}

catch ( ExceptionClass exceptionObject ) {

statement

statement …

}

finally {

statement

statement …

}

KR – CS 1401 Spring 2005

Page 11: Exceptions  Don’t Frustrate Your User – Handle Errors

Exceptions Example: One Catch Block

int number;

try {String userInput = inFile.readLine( );

number = Integer.parseInt ( userInput ); }

catch ( NumberFormatException excep ) { System.out.println ( “Cannot convert input. Please enter integer.” );}

finally {System.out.println ( “End of try-catch-finally statement.” );

}

KR – CS 1401 Spring 2005

Page 12: Exceptions  Don’t Frustrate Your User – Handle Errors

Exceptions Multiple Catch Blocks May Be Defined

try { statement

statement …}catch ( ExceptionClass exceptionObject ) { statement

statement …}catch ( ExceptionClass exceptionObject ) {

statement statement …

}

finally { … }

KR – CS 1401 Spring 2005

Page 13: Exceptions  Don’t Frustrate Your User – Handle Errors

Exceptions Example: Multiple Catch Blocks

int number;try {

String userInput = inFile.readLine( ); number = Integer.parseInt ( userInput ); }

catch ( IOException excep ) { System.out.println ( “Problem reading file.” );}

catch ( NumberFormatException excep ) { System.out.println ( “Cannot convert input. Please enter integer.” );}

finally { … }

KR – CS 1401 Spring 2005

Page 14: Exceptions  Don’t Frustrate Your User – Handle Errors

Exceptions Throwing Our Own Exceptions

int number; // Ask user to enter a number > = 100try {

String userInput = inFile.readLine( ); number = Integer.parseInt ( userInput );

if ( number < 100 ) { throw new Exception (userInput + “must be > = 100” );

} }catch ( NumberFormatException excep ) { System.out.println ( “Cannot convert input. Please enter integer.” );}

catch ( Exception excep ) { System.out.println ( “Error: ” + excep.getMessage ( ) );}finally { … }

KR – CS 1401 Spring 2005

Page 15: Exceptions  Don’t Frustrate Your User – Handle Errors

ExceptionsOrder of Multiple Catch Blocks

List multiple catch blocks in the order of more specialized exception classes to more general exceptions:

catch ( NumberFormatException excep ) { System.out.println ( “Cannot convert input. Please enter integer.” );}

catch ( Exception excep ) { System.out.println ( “Error: ” + excep.getMessage ( ) );}

Only one catch block will be executed; the rest will be skipped.

KR – CS 1401 Spring 2005

Page 16: Exceptions  Don’t Frustrate Your User – Handle Errors

Exceptions Defining Our Own Exception Subclass

int number; // Ask user to enter a number > = 100try {

String userInput = inFile.readLine( ); number = Integer.parseInt ( userInput );

if ( number < 100 ) { throw new ourInputException (userInput + “must be > = 100” );

} }catch ( NumberFormatException excep ) { System.out.println ( “Cannot convert input. Please enter integer.” );}

catch ( ourInputException excep ) { System.out.println ( “Error: ” + excep.getMessage ( ) );}finally { … }

KR – CS 1401 Spring 2005

Page 17: Exceptions  Don’t Frustrate Your User – Handle Errors

Exceptions What is ourInputException ?

It is the subclass of Exception that we defined:

public class ourInputException extends Exception {

public ourInputException ( ) { // Call constructor of Exception Class with no argumentsuper ( ) ;

}

public ourInputException ( String message ) { // Call constructor of Exception Class and pass message that catch

// clause can use for error handling.super ( message );

}

KR – CS 1401 Spring 2005