Skip to content

Commit 0820d44

Browse files
authored
Merge pull request #8 from Nordstrom/pr/fix-unchecked-throw
<T> extends Throwable
2 parents ed0bbd6 + e516e03 commit 0820d44

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/main/java/com/nordstrom/common/base/UncheckedThrow.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static RuntimeException throwUnchecked(final Throwable thrown) {
3636
* @throws T dummy declaration to satisfy the compiler
3737
*/
3838
@SuppressWarnings("unchecked")
39-
private static <T extends Exception> void propagate(Throwable thrown) throws T {
39+
private static <T extends Throwable> void propagate(Throwable thrown) throws T {
4040
// Due to generic type erasure, this cast only serves to satisfy the compiler
4141
// that the requirement to declare the thrown exception has been met.
4242
throw (T) thrown;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.nordstrom.common.base;
2+
3+
import java.io.IOException;
4+
5+
import org.testng.annotations.Test;
6+
7+
public class UncheckedThrowTest {
8+
9+
@Test(expectedExceptions = {IOException.class})
10+
public void testCheckedException() {
11+
try {
12+
throwCheckedException();
13+
} catch (Throwable t) {
14+
throw UncheckedThrow.throwUnchecked(t);
15+
}
16+
}
17+
18+
@Test(expectedExceptions = {AssertionError.class})
19+
public void testUncheckedException() {
20+
try {
21+
throwUncheckedException();
22+
} catch (Throwable t) {
23+
throw UncheckedThrow.throwUnchecked(t);
24+
}
25+
}
26+
27+
private void throwCheckedException() throws IOException {
28+
throw new IOException("This is a checked exception");
29+
}
30+
31+
private void throwUncheckedException() {
32+
throw new AssertionError("This is an unchecked exception");
33+
}
34+
35+
}

0 commit comments

Comments
 (0)