Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Java Programming

Similar presentations


Presentation on theme: "Advanced Java Programming"— Presentation transcript:

1 Advanced Java Programming
Session #, Speaker Name Advanced Java Programming CSS446 Spring 2014 Nan Wang 08/24/11

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

3 Reading and Writing Text Files
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

4

5 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?

6 Common Error (1) Backslashes in File Names

7 Common Error (2) Constructing a Scanner with a String

8 Command Line Arguments

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

10 Catching Exceptions

11

12 Internal Errors 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.

13 Unchecked Exceptions 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

14 Checked Exceptions 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! 

15 The throws Clause

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

17 Designing Your own Exception Types
Describe an error condition, provide a subclass of an existing exception class.

18

19

20


Download ppt "Advanced Java Programming"

Similar presentations


Ads by Google