This is an automated email from the ASF dual-hosted git repository.
hepin pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/pekko.git
The following commit(s) were added to refs/heads/main by this push:
new 9b5e641246 feat: Add sneakyThrow for Java (#2218)
9b5e641246 is described below
commit 9b5e6412464b5e0c8801a6e3150fa0732641173f
Author: He-Pin(kerr) <[email protected]>
AuthorDate: Fri Sep 19 22:44:53 2025 +0800
feat: Add sneakyThrow for Java (#2218)
---
.../src/test/java/org/apache/pekko/japi/ThrowablesTest.java | 9 +++++++++
actor/src/main/scala/org/apache/pekko/japi/Throwables.scala | 11 +++++++++++
2 files changed, 20 insertions(+)
diff --git
a/actor-tests/src/test/java/org/apache/pekko/japi/ThrowablesTest.java
b/actor-tests/src/test/java/org/apache/pekko/japi/ThrowablesTest.java
index 7c7f9ac504..ac7759c6d1 100644
--- a/actor-tests/src/test/java/org/apache/pekko/japi/ThrowablesTest.java
+++ b/actor-tests/src/test/java/org/apache/pekko/japi/ThrowablesTest.java
@@ -29,4 +29,13 @@ public class ThrowablesTest {
Assert.assertTrue(Throwables.isFatal(new InterruptedException("fatal")));
Assert.assertTrue(Throwables.isFatal(new LinkageError("fatal")));
}
+
+ private void doSneakyThrow() {
+ Throwables.sneakyThrow(new Exception("sneaky"));
+ }
+
+ @Test
+ public void testSneakyThrow() {
+ Assert.assertThrows("sneaky", Exception.class, this::doSneakyThrow);
+ }
}
diff --git a/actor/src/main/scala/org/apache/pekko/japi/Throwables.scala
b/actor/src/main/scala/org/apache/pekko/japi/Throwables.scala
index 184b143663..0cab67fe1c 100644
--- a/actor/src/main/scala/org/apache/pekko/japi/Throwables.scala
+++ b/actor/src/main/scala/org/apache/pekko/japi/Throwables.scala
@@ -49,4 +49,15 @@ object Throwables {
* or false if it is to be considered non-fatal
*/
def isFatal(throwable: Throwable): Boolean = !isNonFatal(throwable)
+
+ /**
+ * Throws the given `Throwable`, without requiring the caller to declare it
in a `throws` clause.
+ * @param t the `Throwable` to throw
+ * @throws T the type of the `Throwable` to throw
+ * @return never returns normally, but has return type `R` to allow usage in
expressions
+ * @since 2.0.0
+ */
+ def sneakyThrow[T <: Throwable, R](t: Throwable): R = {
+ throw t.asInstanceOf[T]
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]