Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 Overview – Learn to use try catch blocks – Learn to use streams – Learn to use text files.

Similar presentations


Presentation on theme: "Chapter 8 Overview – Learn to use try catch blocks – Learn to use streams – Learn to use text files."— Presentation transcript:

1 Chapter 8 Overview – Learn to use try catch blocks – Learn to use streams – Learn to use text files

2 Exceptions We have used if statements to Prevent errors – if (count != 0) { average = sum / count; } else { average = 0; System.out.println(“Error – count is zero”); Protects against runtime error

3 Exceptions classes There are a series of classes used to trap exceptions. See table top of page 517 for some typical run time exceptions

4 Try-Catch statement Some errors can not be protected against using if statements Try-catch lets the error occur – if occurs it catches and handles the error readInt uses this – used to catch bad user input See example bottom of page 517

5 New Statement!!! Way Cool Syntax try { //statements that may throw exception} catch(exceptionType parameterName) { // statements for this exception type } ……………. finally { //statements to execute after try/catch }

6 Multiple catch blocks In the previous code you can have multiple catch blocks – allows you to catch multiple exception types finally block is optional executes after try-catch blocks see example page 520 – btw only one catch

7 Catch block order This is important if having multiple catch blocks Need to keep in mind class hierarchy Only first catch block that matches exception is executed. Class Exception is the superclass of all exceptions classes – if it is first it will always be executed since all exceptions are of this type – It should be last to catch anything that may have been missed See example page 522

8 Other helpful methods ex.getMessage() – retrieves message indicating exception type ex.printStackTrace() – shows which step throw error See output bottom of page 523

9 Throwing exceptions We can throw exceptions out of a method, back to the calling method. Use throw clause in header of method Tells compiler that this method can throw and exception The calling program must then catch the exception See example on page 525

10 Checked exceptions Special category of exceptions called checked exceptions You must either – catch these exceptions – or handle by throwing to calling method If you do not you will get syntax error. Subclasses of IOException are checked – Arithmetic are not

11 Throwing your own You can also throw exceptions using the throw statement Would allow you to customize the error message thrown See bottom page 526 example

12 8.2 Streams and Text Files We have been using a stream off and on all semester System.out is a stream to the system console – print – println Streams are just a continuous “stream” of characters, both printable and non-printable.

13 Writing to Output Text File Create Stream object and associate it with disk file Give Stream object desired functionality Read information from an input file or write information to an output file Close the file

14 Write to file See example page 531 – FileWriter outStream …….. creates stream object – PrintWriter outs = new……. Wraps stream object in new object Gives desired functionality Program then writes the data to text file

15 Input/Output Exceptions IOException is the most general class for I/O Not only can you output to system console You can also read from the system console – System.in – readLine Most useful when running from command prompt.

16 8.3 Using Text Files This is what we need to learn to do to complete Major assignment number 4. FileReader inStream = new FileReader(“myData.txt”); BufferedReader ins = new BufferedReader(inStream); Methods we then use are: – ins.readLine(); – ins.ready();

17 How to read text file DVD GUI If we look on page 544 the DVD example The method readDVDs in the middle of the page: – basically does what we need to complete the project – title going to null signifies the end of the file – can also use ready() to find out when file ends We need to read our input file for Major 4, although we do not need to store the records in an array

18 Chapter 8 in closing This is what we need to know out of Chapter 8 Section 8.5 Binary files These files are just files that are storing binary data, not text data. Binary data is stored in native format. Our Major 4 uses a text file that can actually be opened in notePad


Download ppt "Chapter 8 Overview – Learn to use try catch blocks – Learn to use streams – Learn to use text files."

Similar presentations


Ads by Google