Chapter 12: Looking Good When Things Take Unexpected
Tuesday, March 31st, 2009Chapter 12: Looking Good When Things Take Unexpected Turns 309 Figure 12-13: There s a five-second pause before the Ah line. out.println( for just five seconds… ); takeANap(); out.println( Ah, that was refreshing. ); } static void takeANap() { try { Thread.sleep(5000); } catch (InterruptedException e) { out.println( Hey, who woke me up? ); } } } It s my custom, at this point in a section, to remind you that a run of Listing Such-and-Such is shown in Figure So-and-So. But the problem here is that Figure 12-13 doesn t do justice to the code in Listing 12-9. When you run the program in Listing 12-9, the computer displays Excuse me while I nap for just five seconds, pauses for five seconds, and then displays Ah, that was refreshing. The code works because the call to the sleep method, which can throw an InterruptedException, is inside a try clause. That tryclause has a catchclause whose exception is of type InterruptedException. So much for acknowledging an exception with a try-catchstatement. You can acknowledge an exception another way, shown in Listing 12-10. Listing 12-10: Acknowledging with throws import static java.lang.System.out; class GoodNightsSleepB { public static void main(String args[]) { (continued)
If you looking for unlimited one inclusive web hosting plan please check web hosting plan website.