summaryrefslogtreecommitdiffstats
path: root/yolean/src
diff options
context:
space:
mode:
authorBjørn Christian Seime <bjorncs@verizonmedia.com>2020-12-01 17:06:42 +0100
committerBjørn Christian Seime <bjorncs@verizonmedia.com>2020-12-01 17:49:22 +0100
commit0b8d21fe0c915057e6c88cab1b2e988d60c5a16f (patch)
tree3f03f81c21ba25065b1b59e501b2520d65bab98e /yolean/src
parentf2599280846073b144c4065bf25478138cc38b67 (diff)
Add helper method to rethrow checked as unchecked
Diffstat (limited to 'yolean/src')
-rw-r--r--yolean/src/main/java/com/yahoo/yolean/Exceptions.java21
1 files changed, 21 insertions, 0 deletions
diff --git a/yolean/src/main/java/com/yahoo/yolean/Exceptions.java b/yolean/src/main/java/com/yahoo/yolean/Exceptions.java
index 063ba70c75d..c377ee3ac37 100644
--- a/yolean/src/main/java/com/yahoo/yolean/Exceptions.java
+++ b/yolean/src/main/java/com/yahoo/yolean/Exceptions.java
@@ -160,4 +160,25 @@ public class Exceptions {
public interface SupplierThrowingIOException<T> {
T get() throws IOException;
}
+
+ /**
+ * Allows treating checked exceptions as unchecked.
+ * Usage:
+ * throw throwUnchecked(e);
+ * The reason for the return type is to allow writing throw at the call site
+ * instead of just calling throwUnchecked. Just calling throwUnchecked
+ * means that the java compiler won't know that the statement will throw an exception,
+ * and will therefore complain on things such e.g. missing return value.
+ */
+ public static RuntimeException throwUnchecked(Throwable e) {
+ throwUncheckedImpl(e);
+ return new RuntimeException(); // Non-null return value to stop tooling from complaining about potential NPE
+ }
+
+ @SuppressWarnings("unchecked")
+ private static <T extends Throwable> void throwUncheckedImpl(Throwable t) throws T {
+ throw (T)t;
+ }
+
+
}