...
Code Block |
---|
|
try {
//...
} catch (IOException ioe) {
// Ignore
}
|
...
Code Block |
---|
|
try {
//...
} catch (IOException ioe) {
ioe.printStacktrace();
}
|
...
Code Block |
---|
|
boolean volatile validFlag = false;
do {
try {
// If requested file does not exist, throws FileNotFoundException
// If requested file exists, sets a Boolean flag validFlag to true
validFlag = true;
} catch (FileNotFoundException e) {
// Ask the user for a different filename
}
} while(validFlag != true);
// Use the file
|
The user is allowed to access only files in a user-specific directory; consequently this approach avoids leaking . This prevents any other IOException
that escapes the loop from leaking potentially sensitive file system information. See guideline EXC06-J. Do not allow exceptions to expose sensitive information for additional information.
Noncompliant Code Example
The If a thread is interrupted while sleeping or waiting, it causes a java.lang.InterruptedException
to be thrown. But the run()
method of a interface Runnable
object cannot throw a checked exception. Consequently, this , and so it must handle InterruptedException
. This noncompliant code example catches and suppresss java.lang.InterruptedException
.
Code Block |
---|
|
class Foo implements Runnable {
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// Ignore
}
}
}
|
Wiki Markup |
---|
This code prevents callers higher up the call stack from determining that an interrupted exception occurred; consequently, they are unable to act on the exception \[[Goetz 2006|AA. Bibliography#Goetz 06]\]. Likewise, if this code was called in its own thread, it prevents the calling thread from knowing that this thread was interrupted. |
Compliant Solution
This compliant solution catches the InterruptedException
and restores the interrupted status by calling the interrupt()
method on the current thread.
Code Block |
---|
|
class Foo implements Runnable {
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // Reset interrupted status
}
}
}
|
Wiki Markup |
---|
Consequently, code that is higher up on the call stack (or code from a calling thread) can see that an interrupt was issued \[[Goetz 2006|AA. Bibliography#Goetz 06]\]. |
...
Some APIs may limit the permissible exceptions thrown by particular methods. In such cases, it may be necessary to catch an exception and either wrap it in a permitted exception or translate it to one of the permitted exceptions.
...
Code Block |
---|
|
try {
// Requested file does not exist
// User is unable to supply the file name
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
|
Wiki Markup |
---|
*EXC00-EX3:* "The only situation in which it is acceptable to swallow an interrupt is when you are extending Thread and therefore control all the code higher up on the call stack" \[[Goetz 2006|AA. Bibliography#Goetz 06]\]. In such cases {{InterruptedException}} may be caught and suppressd. A interruption request may also be swallowedsuppressed by code that implements a thread's interruption policy \[[Goetz 2006, pg 143|AA. Bibliography#Goetz 06]\]. |
...