CS 195 Course Outline & Introduction to Java

Download Report

Transcript CS 195 Course Outline & Introduction to Java

CSS446 Spring 2014 Nan Wang

    Read and write text files Process command line arguments Throw and catch exceptions Implement programs that propagate checked exceptions 2

     Use the Scanner class for reading text files Use the File class to describe disk files and directories.

Use the PrintWriter class to write text files Close the Scanner and PrintWriter before done processing file (Some of the output may not be written to the disk file) ◦ ◦ ◦ FileNotFoundException occurs when input file or output file does not exists for a scanner When PrintWriter can not open the file to write Label the main method with a throws declararion 3

4

  What happens when you supply the same name for the input and output files to the Total program? What happens when you supply the name of a nonexistent input file to the Total program?

5

 Backslashes in File Names 6

 Constructing a Scanner with a String 7

8

  Two way to deal with the program errors: ◦ ◦ Detection Handling To signal an exceptional condition, use the throw statement to throw an exception object.

9

10

11

  Internal errors are reported by descendants of the type Error. One example is the OutOfMemoryError, which is thrown when all available computer memory has been used up. These are fatal errors that happen rarely, and we will not consider them in this book.

12

  Descendants of RuntimeException, such as as IndexOutOfBoundsException or Illegal ArgumentException indicate errors in your code. They are called unchecked exceptions. And it is your fault  13

 All other exceptions are checked exceptions. These exceptions indicate that something has gone wrong for some external reason beyond your control.

 No matter how careful you are, it happens!  14

15

 Once a try block is entered, the statements in a finally cause are guaranteed to be executed, whether or not an exception is thrown.

16

 Describe an error condition, provide a subclass of an existing exception class.

17

18

19

20