Skip to content

Instantly share code, notes, and snippets.

@jnape
Created September 10, 2016 18:00
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 jnape/3cdb54b5deefcc8c1e575f74868894d7 to your computer and use it in GitHub Desktop.
Save jnape/3cdb54b5deefcc8c1e575f74868894d7 to your computer and use it in GitHub Desktop.
import java.io.IOException;
public class ExceptionalExceptionHandling {
public static void main(String[] args) {
ThrowingSupplier<IllegalStateException, String> wellTyped = () -> {
throw new IllegalStateException();
};
ThrowingSupplier<RuntimeException, String> wellTypedButUnnecessary = () -> {
throw new IllegalStateException();
};
ThrowingSupplier<RuntimeException, String> wellTypedButNotEnforcedBecauseUnchecked = () -> {
if (false) {
throw new IllegalStateException();
} else {
throw new IllegalArgumentException();
}
};
ThrowingSupplier<Exception, String> wellTypedAndEnforcedBecauseChecked = () -> {
if (false) {
throw new IOException();
} else {
throw new NoSuchMethodException();
}
};
ThrowingSupplier<IllegalStateException, String> illTypedAndUncaughtBecauseUnchecked = () -> {
if (false) {
throw new IllegalStateException();
} else {
throw new IllegalArgumentException();
}
};
ThrowingSupplier<IOException, String> illTypedAndUncaughtBecauseMixedCheckedAndUnchecked = () -> {
if (false) {
throw new IOException();
} else {
throw new IllegalArgumentException();
}
};
ThrowingSupplier<RuntimeException, String> illTypedAndCaughtBecauseChecked = () -> {
if (false) {
throw new IOException();
} else {
throw new IllegalArgumentException();
}
};
ThrowingSupplier<IOException, String> illTypedAndCaughtBecauseBothChecked = () -> {
if (false) {
throw new IOException();
} else {
throw new NoSuchMethodException();
}
};
}
@FunctionalInterface
public interface ThrowingSupplier<E extends Exception, T> {
T apply() throws E;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment