Java Reference
In-Depth Information
Example 3.22 Use of a finally clause
try {
for (i = 0; i < max; i++) {
someobj.methodB(param1, i);
} // next i
} catch (SpecialException sp) {
System.out.println(sp.whatWentWrong());
} catch (AlternateException alt) {
alt.attemptRepair(param1);
throw alt; // pass it on
} catch (Exception e) {
// do the error handling here:
System.out.println(e.toString());
e.printStackTrace();
} finally {
// Continue execution here after any catch
// or after a try with no exceptions.
// It will even execute after the AlternateException
// before the throw takes execution away from here.
gone = true;
someobj = null;
}
print() , println() , printf()
3.2.5
We've already used println() in several examples, and assumed that you can
figure out what it's doing from the way we have used it. Without going whole-
hog into an explanation of Java I/O and its various classes, we'd like to say a
little more about the three various output methods on a PrintStream object. 6
Two of the methods, print() and println() , are almost identical. They
differ only in that the latter one appends a newline (hence the ln ) at the end
of its output, thereby also flushing the output. They expect a String as their
only argument, so when you want to output more than one thing, you add the
String s together, as in:
6. The mention of the PrintStream object was meant to be a hint, to tell you that you can
find out more about this sort of thing on the Javadoc pages for the PrintStream object.
Search WWH ::




Custom Search