Skip to content

Instantly share code, notes, and snippets.

@kamarcum
Created July 10, 2012 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamarcum/3085433 to your computer and use it in GitHub Desktop.
Save kamarcum/3085433 to your computer and use it in GitHub Desktop.
Fiddling with error behavior in Java
public abstract class ErrorBehavior<T extends Exception> {
public abstract void handle(T e);
public class SilentErrorBehavior extends ErrorBehavior {
@Override
public void handle(T e) {
}
}
public class DebugErrorBehavior extends ErrorBehavior {
private final Logger log;
public DebugErrorBehavior(Logger log) {
this.log = log;
}
@Override
public void handle(T e) {
log.debug(stringify(e));
}
}
public class ThrowErrorBehavior extends ErrorBehavior {
@Override
public void handle(T e) {
throw new RuntimeException(e); //this isn't very polite Java
}
}
private String stringify(Exception e) {
return e.getMessage(); //maybe other stuff would happen here?
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment