770 likes | 910 Views
Chapter 11. Exceptions and Input/Output Operations. Topics. Exception Handling Using try and catch Blocks Catching Multiple Exceptions User-Defined Exceptions The java.io Package Parsing a String using Scanner Reading Text Files Using Scanner
E N D
Chapter 11 Exceptions and Input/Output Operations
Topics • Exception Handling • Using try and catch Blocks • Catching Multiple Exceptions • User-Defined Exceptions • The java.io Package • Parsing a String using Scanner • Reading Text Files Using Scanner • Writing and Appending to Text Files • Reading Structured Text Files • Reading and Writing Objects to a File
Exceptions Illegal operations at run time can generate an exception. For example, we have seen these exceptions: • ArithmeticException • NullPointerException • InputMismatchException • NumberFormatException • ArrayIndexOutOfBoundsException
Exceptions • An exception is an object that describes an unusual or erroneous situation • Exceptions are thrown by a program, and may be caught and handled by another part of the program • A program can be separated into a normal execution flow and an exception execution flow • An error is also represented as an object in Java, but usually represents a unrecoverable situation and should not be caught
Handling Exceptions In a program without a Graphical User Interface, exceptions cause the program to terminate. With this code that attempts to read an int: String s = JOptionPane.showInputDialog( null, "Enter an integer" ); int n = Integer.parseInt( s ); If the user enters a, we get a NumberFormatException: See Example 11.1 DialogBoxInput.java
Exception Handling • Java has a predefined set of exceptions and errors that can occur during execution • A program can deal with an exception in one of three ways: • ignore it • handle it where it occurs • handle it an another place in the program • The manner in which an exception is processed is an important design consideration
Exception Handling • If an exception is ignored by the program, the program will terminate abnormally and produce an appropriate message • The message includes a call stack trace that: • indicates the line on which the exception occurred • shows the method call trail that lead to the attempted execution of the offending line
Handling Exceptions We don't want invalid user input to terminate our programs! It is better to detect the problem and reprompt the user for the input. Java allows us to intercept and handle some of these exceptions using try and catch blocks. • Inside the try block, we put the code that might generate an exception. • Inside catch blocks, we put the code to handle any exceptions that could be generated.
Minimum try/catch Syntax try { // code that might generate an exception } catch( ExceptionClass exceptionObjRef ) { // code to recover from the exception } • If an exception occurs in the try block, control jumps immediately to the catch block. No further instructions in the try block are executed. • If no exceptions are generated in the try block, the catch block is not executed.
Exception Class Hierarchy The ExceptionClass parameter to the catch block can be any of these exceptions. The Exception class, RuntimeException class, and their subclasses are in the java.lang package. The IOException class and its subclasses are in the java.io package.
Checked and Unchecked Exceptions Java distinguishes between two types of exceptions: Unchecked exceptionsare those that are subclasses of Error or RuntimeException • It is not mandatory to use try and catch blocks to handle these exceptions. Checked exceptions are any other exceptions. • Code that might generate a checked exception must be put inside a try block or the method must acknowledge that the exception may occur by using a throws clause in the method header. Otherwise, the compiler will generate an error.
Checked Exceptions • An exception is either checked or unchecked • A checked exception either must be caught by a method, or must be listed in the throws clause of any method that may throw or propagate it • A throws clause is appended to the method header • The compiler will issue an error if a checked exception is not caught or asserted in a throws clause
Unchecked Exceptions • An unchecked exception does not require explicit handling, though it could be processed that way • The only unchecked exceptions in Java are objects of type RuntimeException or any of its descendants • Errors are similar to RuntimeException and its descendants in that: • Errors should not be caught • Errors do not require a throws clause
Exception Class Methods Inside the catch block, you can call any of these methods of the Exception class:
Catching a NumberFormatException int n = 0; // declare and initialize variable String s = JOptionPane.showInputDialog( null, "Enter an integer" ); try { n = Integer.parseInt( s ); System.out.println( "You entered " + n ); } catch ( NumberFormatException nfe ) { System.out.println( "Incompatible data." ); } See Example 11.2 DialogBoxInput.java
Initializing Variables for try/catch Notice that we declare and initialize the input variable (n) before we enter the try block. If we do not initialize the variable and then try to accessit after the try/catch blocks, we will receive the following compiler error: variable n might not have been initialized The error indicates that the only place where n is assigned a value is in the try block. If an exception occurs, the try block will be interrupted and we might not ever assign n a value. Initializing the value before entering the try block solves this problem.
Recovering From an Exception The previous code simply printed a message when the exception occurred. To continue processing, reprompt the user for good input by putting the try and catch blocks inside a do/while loop, as shown on the next slide -> See Example 11.3 DialogBoxInput.java
int n = 0; boolean goodInput = false; // flag variable String s = JOptionPane.showInputDialog( null, "Enter an integer" ); do { try { n = Integer.parseInt( s ); goodInput = true; // executed if no exception } catch ( NumberFormatException nfe ) { s = JOptionPane.showInputDialog( null, s + " is not an integer. " + "Enter an integer" ); } } while ( ! goodInput );
SOFTWARE ENGINEERING TIP Write code to catch and handle exceptions generated by invalid user input. Always try to write code that is user-friendly. Although the methods of the Exception class are good debugging tools, they are not necessarily appropriate to use in the final version of a program.
Catching Multiple Exceptions If the code in the try block might generate multiple, different exceptions, we can provide multiple catch blocks to handle each possible exception. When an exception is generated,the JVM searches the catch blocks in order.The first catch block with a parameter that matches the exception thrownwill execute; any remaining catch blocks will be skipped.
catch Block Order An exception will match a catch block with a parameter that names any of its superclasses. • For example, a NumberFormatException will match a catch block with a RuntimeException parameter. • All exceptions will match a catch block with an Exception parameter. Thus, when coding several catch blocks, arrange the catch blocks with the specialized exceptions first, followed by more general exceptions.
Exception Propagation • An exception can be handled at a higher level if it is not appropriate to handle it where it occurs • Exceptions propagate up through the method calling hierarchy until they are caught and handled or until they reach the level of the main method • A try block that contains a call to a method in which an exception is thrown can be used to catch that exception
The finally Block Optionally, you can follow the catch blocks with a finally block. • The finally block will be executed whether or not an exception occurs. Thus: • if an exception occurs, the finally block will be executed when the appropriate catch block finishes executing. • if no exception occurs, the finally block will be executed when the try block finishes. • For example, a finally block might be used to close an open file. We demonstrate this later.
Full try/catch/finally Syntax try { //code that might generate an exception } catch( Exception1Class e1 ) { //code to handle an Exception1Class exception } … catch( ExceptionNClass eN ) { //code to handle an ExceptionNClass exception } finally { //code to execute whether or not an exception occurs }
Catching Multiple Exceptions We can write a program that catches several exceptions. For example, for a division operation, we can prompt the user for a divisor. • If the input is not an integer, we catch the NumberFormatException and reprompt the user with an appropriate message. • If the input is 0, we catch an ArithmeticException when we attempt to divide by 0, and reprompt the user with an appropriate message. See Example 11.4 Divider.java
Example 11.4 // declare and initialize variables int divisor = 0; int quotient = 0; int dividend = 100; // initialize flag variable boolean goodInput = false; // prompt for input String s = JOptionPane.showInputDialog( null, "Enter an integer divisor" ); do { try { // attempt to convert the String to an int divisor = Integer.parseInt( s ); // attempt the division quotient = dividend / divisor; goodInput = true; } catch ( NumberFormatException nfe ) { s = JOptionPane.showInputDialog( null, s + " is not an integer. “ + "Enter an integer divisor" ); } catch ( ArithmeticException ae ) { s = JOptionPane.showInputDialog( null, "Divisor cannot be 0. “ + "Enter an integer divisor" ); } } while ( !goodInput ); JOptionPane.showMessageDialog( null, "The result is " + quotient );
User-Defined Exceptions We can design our own exception class. • Suppose we want to design a class encapsulating email addresses (EmailAddress class). • For simplicity, we say that a legal email address is a String containing the @ character. • Our EmailAddress constructor will throw an exception if its email address argument is illegal. • To do this, we design an exception class named IllegalEmailException.
User-Defined Exception • Java has an IllegalArgumentException class, so our IllegalEmailException class can be a subclass of the IllegalArgumentException class. • By extending the IllegalArgumentException class: • we inherit the functionality of an exception class, which simplifies our coding of the exception • we can easily associate a specific error message with the exception
Extending an Existing Exception We need to code only the constructor, which accepts the error message as a String. General pattern: public class ExceptionName extends ExistingExceptionClassName { public ExceptionName( String message ) { super( message ); } } See Example 11.5 IllegalEmailException.java
Example 11.5 public class IllegalEmailException extends IllegalArgumentException { public IllegalEmailException( String message ) { super( message ); } }
The throw Statement • Exceptions are thrown using the throw statement • Usually a throw statement is executed inside an if statement that evaluates a condition to see if the exception should be thrown
Throwing an Exception The pattern for a method that throws a user-defined exception is: accessModifier returnType methodName( parameters ) throws ExceptionName { if ( parameter list is legal ) process the parameter list else throw new ExceptionName( "Message here" ); } The message passed to the constructor identifies the error detected. In a client's catch block, the getMessage method retrieves that message. See Examples 11.6 EmailAddress.java & 11.7 EmailChecker.java
Example 11.6 public class EmailAddress { public static final char AT_SIGN = '@'; private String email; public EmailAddress( String newEmail ) throws IllegalEmailException { if ( newEmail.indexOf( AT_SIGN ) != - 1 ) email = newEmail; else throw new IllegalEmailException ( "Email address does not contain " + AT_SIGN ); } public String getHost( ) { int index = email.indexOf( AT_SIGN ); return email.substring( index + 1, email.length( ) ); } }
Example 11.7 public class EmailChecker { public static void main( String [ ] args ) { Scanner scan = new Scanner( System.in ); System.out.print( "Enter your email address > " ); String myEmail = scan.next( ); try { EmailAddress address = new EmailAddress( myEmail ); System.out.println( "Your host is " + address.getHost( ) ); } catch( IllegalEmailException iee ) { System.out.println( iee.getMessage( ) ); } } }
I/O Exceptions • Let's examine issues related to exceptions and I/O • A stream is a sequence of bytes that flow from a source to a destination • In a program, we read information from an input stream and write information to an output stream • A program can manage multiple streams simultaneously
Standard I/O • There are three standard I/O streams: • standard output – defined by System.out • standard input – defined by System.in • standard error – defined by System.err • We use System.out when we execute println statements • System.out and System.err typically represent a particular window on the monitor screen • System.in typically represents keyboard input, which we've used many times with Scanner objects
The IOException Class • Operations performed by some I/O classes may throw an IOException • A file might not exist • Even if the file exists, a program may not be able to find it • The file might not contain the kind of data we expect • An IOException is a checked exception
File Types • Java supports two types of files: • text files: data is stored as characters • binary files: data is stored as raw bytes • The type of a file is determined by the classes used to write to the file. • To read an existing file, you must know the file's type in order to select the appropriate classes for reading the file.
Reading Text Files In Chapter 6, we read a text file by associating a Scanner object with a File object: Scanner file = new Scanner( new File( “filename.txt” ) ); We added the throws IOException clause to main to avoid handling exceptions that may occur. public static void main( String [] args ) throws IOException But when the file wasn’t found, a FileNotFoundException occurred; the user was given an unfriendly message; and the program terminated. Now that we know how to catch exceptions, we can intercept the FileNotFoundException and give the user a meaningful message.
Opening and Closing an InputStream • When we construct an input stream or output stream object, the JVM associates the file name, standard input stream, or standard output stream with our object. This is opening the file. • When we are finished with a file, we optionally call the close method to release the resources associated with the file.
Opening and Closing Standard Streams The standard input stream (System.in), the standard output stream (System.out), and the standard error stream (System.err) are open when the program begins.They are intended to stay open and should not be closed.
SOFTWARE ENGINEERING TIP Calling the close method is optional. When the program finishes executing, all the resources of any unclosed files are released. It is good practice to call the close method, however, especially if you will be opening a number of files (or opening the same file multiple times.) Do not close the standardinput, output, or error devices, however. They are intended to remain open.
Exceptions While Reading from a File We can catch this exception: FileNotFoundException thrown by the Scanner constructor if the filename is not found when opening the file We do not expect these exceptions to occur, so we will catch them as subclasses of IOException, and print the stack trace. InputMismatchException if the input does not match the expected data type. (The next method does not throw this exception, so we don’t need to catch this exception). NoSuchElementException if we attempt to read beyond the end of the file. IllegalStateException if we attempt to read after calling the close method. See Example 11.8 ReadTextFile.java
Writing to Text Files • Several situations can exist: • the file does not exist • the file exists and we want to replace the current contents • the file exists and we want to append to the current contents • We specify whether we want to replace the contents or append to the current contents when we construct our FileOutputStream object.
Writing Text Files • Previously, we explored the use of the Scanner class to read input from a text file • Let's now examine other classes that let us write data to a text file • The FileWriter class represents a text output file, but with minimal support for manipulating data • Therefore, we also rely on PrintStream objects, which have print and println methods defined for them
Writing Text Files • Finally, we'll also use the PrintWriter class for advanced internationalization and error checking • We build the class that represents the output file by combining these classes appropriately • Output streams should be closed explicitly