


Definition: Makes a task "safe" in that all Exceptions are caught and handled. They might all be handed in some default way or each handled in different ways. Note: Safe Task is not a standard design pattern.
Usage: Use a safe task when you need to know when a task fails to execute properly.
abstract class SafeRunnable implements Runnable {
public SafeRunnable(final String name) {
this.name = name;
}
//a safe version of Runnable's run method
public final void run() {
try {
runSafely();
} catch(Throwable throwable) {
output an error that includes the SafeRunnable's name
}
}
/**The method that will be run within a try-catch block.
*/
protected abstract void runSafely();
String name;
} //end class SafeRunnable
This class can be extended to create a Runnable that catches any Throwables (Errors and Exceptions). This useful in concurrent programs. Without the try-catch block a thread running a Runnable that hits an Exception might mysteriously disappear. With the try-catch block, there will at least be an error message when this happens.
Copyright (C) 2009 Steven Fletcher. All rights reserved.