diff options
author | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2020-12-01 17:06:42 +0100 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2020-12-02 10:10:27 +0100 |
commit | c04726727464a042c3ce0c0bad9b8935eb9b5d8f (patch) | |
tree | 3cb85f988d2464f808a129365cc09bdc5f52cadd /yolean | |
parent | 653a83398e536c0413a4f2ee50981cbe3a6f12f7 (diff) |
Add helper method to rethrow checked as unchecked
Diffstat (limited to 'yolean')
-rw-r--r-- | yolean/abi-spec.json | 3 | ||||
-rw-r--r-- | yolean/src/main/java/com/yahoo/yolean/Exceptions.java | 21 |
2 files changed, 23 insertions, 1 deletions
diff --git a/yolean/abi-spec.json b/yolean/abi-spec.json index 4b68b2527b8..82bf59ebf87 100644 --- a/yolean/abi-spec.json +++ b/yolean/abi-spec.json @@ -40,7 +40,8 @@ "public static void uncheckAndIgnore(com.yahoo.yolean.Exceptions$RunnableThrowingIOException, java.lang.Class)", "public static java.lang.Object uncheck(com.yahoo.yolean.Exceptions$SupplierThrowingIOException)", "public static varargs java.lang.Object uncheck(com.yahoo.yolean.Exceptions$SupplierThrowingIOException, java.lang.String, java.lang.String[])", - "public static java.lang.Object uncheckAndIgnore(com.yahoo.yolean.Exceptions$SupplierThrowingIOException, java.lang.Class)" + "public static java.lang.Object uncheckAndIgnore(com.yahoo.yolean.Exceptions$SupplierThrowingIOException, java.lang.Class)", + "public static java.lang.RuntimeException throwUnchecked(java.lang.Throwable)" ], "fields": [] }, 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; + } + + } |