180 likes | 195 Views
In this chapter overview, you will learn the concepts of try-catch blocks, streams, and text files in Java. Understand how to use exceptions to handle errors, work with streams for input and output, and read from and write to text files. Explore techniques for throwing and catching exceptions, as well as working with checked exceptions. Discover how to use streams to manipulate data, and understand the basics of reading and writing text files.
E N D
Chapter 8 • Overview • Learn to use try catch blocks • Learn to use streams • Learn to use text files
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
Exceptions classes • There are a series of classes used to trap exceptions. • See table top of page 517 for some typical run time exceptions
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
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 }
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
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
Other helpful methods • ex.getMessage() • retrieves message indicating exception type • ex.printStackTrace() • shows which step throw error • See output bottom of page 523
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
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
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
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.
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
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
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.
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();
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
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