Title: Big Java
1Big Java
2Before we begin
- Testing questions
- white box
- test case exercise
- Why does it matter?
- http//it.slashdot.org/it/07/09/24/2339203.shtml
- GUI questions
- Better layouts, button sizing
- Other dialog layout
- JOptionPane for now (not user-friendly)
- learn to Google
- example at end of slides
3- Chapter 11
- Input/Output and
- Exception Handling
4Reading Writing Text Files
- import java.io. // FileReader, exception and
PrintWriter - import java.util.Scanner
- public class LineNumberer
- public static void main(String args) throws
FileNotFoundException -
- Scanner console new Scanner(System.in)
- System.out.print("Input file ")
- String inputFileName console.next()
- System.out.print("Output file ")
- String outputFileName console.next()
- FileReader reader new FileReader(inputFile
Name) - Scanner in new Scanner(reader)
- PrintWriter out new PrintWriter(outputFile
Name) // overwrites - int lineNumber 1
-
- while (in.hasNextLine() )
-
- String line in.nextLine()
5More File I/O Options
- Can hardcode a filename
- in new fileReader(c\\hwk\\in.dat)
- Can display a file dialog (more convenient GUI
for casual user) - JFileChooser chooser new JFileChooser()
- FileReader reader null
- if (chooser.showOpenDialog(null)
- JFileChooser.APPROVE_OPTION)
-
- File selectedFile chooser.getSelectedFile()
- reader new FileReader(selectedFile)
-
- Can be a command line argument (easier to
automate) - if (args.length gt 1)
- inputFileName args0
6Eclipse New Run Configuration
- Select Run icon
- Press New Configuration Icon (be sure Java
application is selected)
Fill in Name, Project and Main Class as needed
7Eclipse Arguments
- Click on Arguments tag to add Program Arguments
8What to do about errors?
- Exceptions handled in try/catch block
- try
-
- String filename . . .
- FileReader reader new FileReader(filename)
- Scanner in new Scanner(reader)
- String input in.next()
- int value Integer.parseInt(input)
-
- catch (FileNotFoundException exception)
- System.out.println(File not found)
-
- catch (IOException e) // would also catch
FileNotFound - e.printStackTrace() // catches
NoSuchElementException -
- catch (NumberFormatException e)
- System.out.println(Input was not a number)
-
-
9Quality Tip
- Throw Early, Catch Late. Better to throw an
exception than come up with an imperfect fix.
What should program do if a file is not found?
Is it always an error? - Do not squelch exceptions.
- try
-
- FileReader reader new FileReader(filename)
-
- catch (Exception e) // So there!
10Checked and Unchecked
- Checked exceptions cant ignore, compiler
requires that you handle or propagate - Unchecked exceptions can be ignored. Generally
subclass of RuntimeException, like
NumberFormatException or NullPointerException.
Too expensive to check all of these, often due to
programmer error (checked exceptions often user
error, like InputMismatchException) - If you arent going to handle a checked
exception, put throws clause on function header - public void read(String filename) throws
FileNotFoundException - . . .
11Exception Hierarchy
Throwable
Exception
Error
IOException
RuntimeException
ClassNot FoundException
CloneNot Supported Exception
ArithmeticException
ClassCastException
EOFException
IllegalStateException
FileNotFoundException
NumberFormatException
MalformedURLException
IndexOutOfBoundsException
UnknownHostException
ArrayIndexOutOfBoundsException
NoSuchElementException
NullPointerException
12Finally Clause
- Sometimes cleanup is needed even after an
exception occurs - PrintWriter out new PrintWriter(filename)
- try
- writeData(out)
-
- finally
-
- out.close() // always executed, including after
- // exception is handled in a catch
- Quality tip dont mix catch and finally in the
same try statement, see tip 11.3
13Designing your own Exception
- public class InsufficientFundsException
- extends RuntimeException
-
- public InsufficientFundsException()
- public InsufficientFundsException (String
message) -
- super(message)
-
-
-
Customary to have two constructors
Must decide which to extend external event or
programmer? If programmer should prevent, make
unchecked (Runtime)
14Example of Exception Handling
main handles all exceptions
15Example (continued)
Exceptions passed back to main to handle
Ensures resources are cleaned up
User-defined exception
16Example (continued)
BadDataException thrown with appropriate
errors for specific problems with input file (no
initial length, too many value, not enough
values)
17Reading Assignment
- Read Random Fact 11.1 The Ariane Rocket
Incident, page 520 for next Tuesday
18Custom Dialog
Creates custom dialog
19Custom Dialog (continued)