Java Reference
In-Depth Information
Finally, note the break at the end of the try statement. If an exception is generated,
then the try block will stop executing and the point of control will be transferred to one
of the catch blocks. After the catch block finishes executing, the infinite while loop will
bring us back to the beginning of the loop. Alternatively, if the code goes through the whole
try block without an exception being raised, then the break statement will transfer control
to the line immediately after the while statement.
An obvious question is when to use the try statement and when to use the throws
keyword. Undoubtedly, using the throws keyword is more convenient. We do not have to
handle the exception. The calling method will take care of it. However, in certain situations it
is impossible to use the throws keyword. For example, consider the paintComponent method
inside a class that inherits from the JPanel class. Since the original paintComponent method
of the JPanel class does not throw an exception, neither can our method that overrides the
original method. In different situations, we do not want a method to throw an exception
because it is extra work for the calling method. For example, if the calling method does not
handle the exception, a compile-time error will be generated.
Lastly, let us consider checked exceptions . These are exceptions that Java forces us to
handle. Opening a file is an example of a task that can potentially generate a checked
exception. Therefore, we must use a try - catch block or add the throws keyword to the
method in order to handle the opening of the file. Alternatively, starting with Java 7, the
following syntax is also allowed.
try (statement that opens f i le ) {
// do something with f i l e
catch (IOException exception) {
// handle I/O problems .
If the file is not opened successfully, then one of the catch blocks is executed. Note
that this syntax also closes the file automatically, which saves us some code writing. The
above syntax is referred to as try -with-resources. The try statement tries to allocate some
resource. If the resource is successfully allocated, it is automatically freed at the end. Oth-
erwise, one of the catch blocks and/or the finally block is executed.
13.2 Text Files
Our poor man's Notepad program from the last chapter seems pretty limited. We cannot
even save our text! Let us go ahead and see how we can extend our program to allow for
saving the text in a text file and reading the text from a text file.
13.2.1 The File Chooser Dialog
There is a class called JFileChooser that displays the usual open file dialog; see Fig-
ure 13.1. It allows the user to select the file that they want opened.
In order to use this class, one first needs to create an object from it.
JFileChooser fileChooser = new JFileChooser () ;
Next, one needs to call the showDialog method on the file chooser. This creates a
file chooser dialog. The method takes as input a reference to the parent window and
the text that will be displayed on the select button. This is the button that needs to
Search WWH ::

Custom Search