Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net."— Presentation transcript:

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

2 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

3 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

4 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

5 Exceptions Classes 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

6 Exceptions Inheritance Diagram of Exception Classes KR – CS 1401 Spring 2005 Diagram – Sun Microsystems

7 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

8 Exceptions Exception handling KR – CS 1401 Spring 2005 Diagrams – Sun Microsystems

9 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

10 Exceptions Syntax of try-catch-finally statement try { statement statement … } catch ( ExceptionClass exceptionObject ) { statement statement … } finally { statement statement … } KR – CS 1401 Spring 2005

11 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

12 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

13 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

14 Exceptions Throwing Our Own Exceptions int number; // Ask user to enter a number > = 100 try { 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

15 Exceptions Order 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

16 Exceptions Defining Our Own Exception Subclass int number; // Ask user to enter a number > = 100 try { 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

17 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 argument super ( ) ; } 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


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

Similar presentations


Ads by Google