Created
September 10, 2016 18:00
-
-
Save jnape/3cdb54b5deefcc8c1e575f74868894d7 to your computer and use it in GitHub Desktop.
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
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