Created
July 10, 2012 18:41
-
-
Save kamarcum/3085433 to your computer and use it in GitHub Desktop.
Fiddling with error behavior in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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