Lecture 34
Checked vs Unchecked Exceptions
There are two main categories of Java exceptions: checked and unchecked.
All of the exceptions we've seen in class so far are considered unchecked. This means
that handling them is optional. We don't have to do anything special in the code where these
exceptions may occur.
There are also checked exceptions in Java. In the cases where we call a method that
may throw a checked exception, we must tell Java what to do with the exception.
There are two ways we can do this. We've seen one already, we can wrap the relevant code in a
try-catch block. We'll see the other technique soon.
Streams
A Java Stream is what we use in Java to move data either into or out of our program.
For example, it is often convenient to save program information to a file, so that the work
can be resumed at a later time. In Java, we use a Stream object to access a file.
To read from a file:
- Create a FileReader passing in the name of the file. A FileReader can only
read one file at a time, so it's not very useful.
- Create a BufferedReader passing in the FileReader from step 1.
- Loop through the file reading each line by calling readLine(). You are done
reading the file when readLine() returns null.
- Close the file by calling close()
Lots of things can go wrong when we try to access files on the disk. For example:
- We might try to read from a file that doesn't exist.
- We might try to read or write a file that we don't have the correct permissions for.
- We might try to write to a file when the disk is full.
All of these problems will cause an IOException to be thrown. This is important
because IOExceptions are checked exceptions. That means we have to handle them.
Writing to a file requires similar steps to reading:
- Create a FileWriter passing in the name of the file.
- Write a String to the file by calling write(String s) passing in the
String as an argument.
- Close the file by calling close().
Application instead of Applets
So far, we've been writing most, if not all, of our programs as Applets. However, Applets are
restricted from accessing files on the disk, so in order to read or write from a file, we
need to write and Application instead.
Writing Applications differs from Applets in several ways:
- We do not extend any of WindowController, Controller, or JApplet. In fact,
It is not necessary to extend anything.
- The main class -- generally, the one that creates the GUI -- needs a method declared as:
public static void main(String args[])
This method should just construct an instance of the class it is in.
- The code you put in the begin() method of your controller or the init() method
of an Applet should be placed in the constructor.
- The constructor needs to create a window to put the GUI components in:
JFrame window = new JFrame();
- You add GUI components to this window's content pane:
Container contentPane = window.getContentPane();
- After you have added all the components, you call pack() to have Java set the
window to the right size. (There is no need to set size parameters in Eclipse's Run dialog box).
You then call setVisible(true) to display the window.
The class example
ShortWordsFixedFiles demonstrates all of the issues discussed above.