With Lombok
01 import lombok.Rethrow
02
03 class RethrowExample {
04 @Rethrow
05 void test1() {
06 System.out.println("code that might throw all kinds of Exceptions");
07 }
08
09 @Rethrow(value = InterruptedException.class, message = "meh.")
10 void test2() {
11 System.out.println("code that might throw InterruptedException due to cancelation");
12 }
13
14 @Rethrow(value = {java.io.FileNotFoundException.class, NullPointerException.class}, as = IllegalArgumentException.class);
15 void test3(final File file) {
16 new java.io.FileInputStream(file);
17 }
18 }
Vanilla Java
01 class RethrowExample {
02
03 void test1() {
04 try {
05 System.out.println("code that might throw all kinds of Exceptions");
06 } catch (java.lang.RuntimeException $e1) {
07 throw $e1;
08 } catch (java.lang.Exception $e2) {
09 throw new java.lang.RuntimeException($e2);
10 }
11 }
12
13 void test2() {
14 try {
15 System.out.println("code that might throw InterruptedException due to cancelation");
16 } catch (java.lang.InterruptedException $e1) {
17 throw new java.lang.RuntimeException("meh.", $e1);
18 }
19 }
20
21 void test3(final File file) {
22 try {
23 new java.io.FileInputStream(file);
24 } catch (java.io.FileNotFoundException $e1) {
25 throw new java.lang.IllegalArgumentException();
26 } catch (java.lang.NullPointerException($e2) {
27 throw new java.lang.IllegalArgumentException($e2);
28 }
29 }
30 }