13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

78Chapter 3An Experienced Programmer’s Introducti<strong>on</strong> to JavaExample 3.22 Use of a finally clausetry {for (i = 0; i < max; i++) {someobj.methodB(param1, i);} // next i} catch (SpecialExcepti<strong>on</strong> sp) {System.out.println(sp.whatWentWr<strong>on</strong>g());} catch (AlternateExcepti<strong>on</strong> alt) {alt.attemptRepair(param1);throw alt; // pass it <strong>on</strong>} catch (Excepti<strong>on</strong> e) {// do the error handling here:System.out.println(e.toString());e.printStackTrace();} finally {// C<strong>on</strong>tinue executi<strong>on</strong> here after any catch// or after a try with no excepti<strong>on</strong>s.// It will even execute after the AlternateExcepti<strong>on</strong>// before the throw takes executi<strong>on</strong> away from here.g<strong>on</strong>e = true;someobj = null;}3.2.5 print(), println(), printf()We’ve already used println() in several examples, and assumed that you canfigure out what it’s doing from the way we have used it. Without going wholehoginto an explanati<strong>on</strong> of Java I/O and its various classes, we’d like to say alittle more about the three various output methods <strong>on</strong> a PrintStream object. 6Two of the methods, print() and println(), are almost identical. Theydiffer <strong>on</strong>ly in that the latter <strong>on</strong>e appends a newline (hence the ln) at the endof its output, thereby also flushing the output. They expect a String as their<strong>on</strong>ly argument, so when you want to output more than <strong>on</strong>e thing, you add theStrings together, as in:6. The menti<strong>on</strong> of the PrintStream object was meant to be a hint, to tell you that you canfind out more about this sort of thing <strong>on</strong> the Javadoc pages for the PrintStream object.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!