Java Reference
In-Depth Information
Discussion Point: Handling Exceptions Not Listed in the
Supplied Interface
You may find that in order to create your Data class, you must call some methods that throw
exceptions that are not listed in the interface supplied by Sun. You will then have to decide
what to do with those exceptions, one by one. There is no single perfect solution for how to
handle this (although there are some bad solutions).
You will have to decide for yourself which approach is right for you. Look at the instruc-
tions you downloaded from Sun (remember, each set of instructions can be different in small
ways), and then decide how best to meet your requirements. Whatever you decide, be sure to
document your decision in your design decisions document, as well as in the source code
itself.
To help illustrate the various possibilities, let's use the following base code:
1 import java.util.logging.*;
2
3 public class InterruptedExceptionExample extends Thread {
4 static Logger log = Logger.getAnonymousLogger();
5
6 public static void main(String[] args) throws InterruptedException {
7 InterruptedExceptionExample iee = new InterruptedExceptionExample();
8 iee.start();
9
10 while (iee.isAlive()) {
11 log.info("main: waiting 5 seconds for other thread to finish");
12 iee.join(5000);
13
14 if (iee.isAlive()) {
15 log.info("main: interrupting other thread.");
16 iee.interrupt();
17 }
18 }
19 log.info("main: finished");
20 }
21
22 public void run() {
23 try {
24 getLock();
25 } catch (LockAttemptFailedException dle) {
26 log.log(Level.WARNING, "Lock attempt failed", dle);
27 }
28 }
29
30 public void getLock() throws LockAttemptFailedException {
31 // try to get some resource that we will presumably never get.
32 for (;;) {
33 try {
Search WWH ::




Custom Search