Created
October 23, 2017 10:15
-
-
Save myui/9722c1301434a3b69cf898ccd9090ff1 to your computer and use it in GitHub Desktop.
Throw Exceptions in Consumer in Java 8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Licensed to the Apache Software Foundation (ASF) under one | |
* or more contributor license agreements. See the NOTICE file | |
* distributed with this work for additional information | |
* regarding copyright ownership. The ASF licenses this file | |
* to you under the Apache License, Version 2.0 (the | |
* "License"); you may not use this file except in compliance | |
* with the License. You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, | |
* software distributed under the License is distributed on an | |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
* KIND, either express or implied. See the License for the | |
* specific language governing permissions and limitations | |
* under the License. | |
*/ | |
package hivemall.utils.lambda; | |
import java.util.function.Consumer; | |
import javax.annotation.Nonnull; | |
public final class Throwing { | |
private Throwing() {} | |
@Nonnull | |
public static <T> Consumer<T> rethrow(@Nonnull final ThrowingConsumer<T> consumer) { | |
return consumer; | |
} | |
/** | |
* The compiler sees the signature with the throws T inferred to a RuntimeException type, so it | |
* allows the unchecked exception to propagate. | |
* | |
* http://www.baeldung.com/java-sneaky-throws | |
*/ | |
@SuppressWarnings("unchecked") | |
@Nonnull | |
public static <E extends Throwable> void sneakyThrow(@Nonnull Throwable ex) throws E { | |
throw (E) ex; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Licensed to the Apache Software Foundation (ASF) under one | |
* or more contributor license agreements. See the NOTICE file | |
* distributed with this work for additional information | |
* regarding copyright ownership. The ASF licenses this file | |
* to you under the Apache License, Version 2.0 (the | |
* "License"); you may not use this file except in compliance | |
* with the License. You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, | |
* software distributed under the License is distributed on an | |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
* KIND, either express or implied. See the License for the | |
* specific language governing permissions and limitations | |
* under the License. | |
*/ | |
package hivemall.utils.lambda; | |
import java.util.function.Consumer; | |
@FunctionalInterface | |
public interface ThrowingConsumer<T> extends Consumer<T> { | |
@Override | |
default void accept(final T e) { | |
try { | |
accept0(e); | |
} catch (Throwable ex) { | |
Throwing.sneakyThrow(ex); | |
} | |
} | |
void accept0(T e) throws Throwable; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Licensed to the Apache Software Foundation (ASF) under one | |
* or more contributor license agreements. See the NOTICE file | |
* distributed with this work for additional information | |
* regarding copyright ownership. The ASF licenses this file | |
* to you under the Apache License, Version 2.0 (the | |
* "License"); you may not use this file except in compliance | |
* with the License. You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, | |
* software distributed under the License is distributed on an | |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
* KIND, either express or implied. See the License for the | |
* specific language governing permissions and limitations | |
* under the License. | |
*/ | |
package hivemall.utils.lambda; | |
import static hivemall.utils.lambda.Throwing.rethrow; | |
import java.io.IOException; | |
import java.util.Arrays; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import org.junit.rules.ExpectedException; | |
public class ThrowingTest { | |
@Rule | |
public ExpectedException thrown = ExpectedException.none(); | |
@Test | |
public void testRethrow() { | |
thrown.expect(IOException.class); | |
thrown.expectMessage("i=3"); | |
Arrays.asList(1, 2, 3).forEach(rethrow(e -> { | |
int i = e.intValue(); | |
if (i == 3) { | |
throw new IOException("i=" + i); | |
} | |
})); | |
} | |
@Test(expected = IOException.class) | |
public void testSneakyThrow() { | |
Throwing.sneakyThrow(new IOException()); | |
} | |
@Test | |
public void testThrowingConsumer() { | |
thrown.expect(IOException.class); | |
thrown.expectMessage("i=3"); | |
Arrays.asList(1, 2, 3).forEach((ThrowingConsumer<Integer>) e -> { | |
int i = e.intValue(); | |
if (i == 3) { | |
throw new IOException("i=" + i); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice :)
Shame you can't just use
<E extends Throwable> void acceptThrows(T elem) throws E;
inThrowableConsumer
and bypassThrowing.sneakyThrow