junit-jupiter-api/src/main/java/org/junit/jupiter/api/function/ThrowingConsumer.java
raw symbols SCIP| 1 | /* |
| 2 | * Copyright 2015-2024 the original author or authors. |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made available under the terms of the Eclipse Public License v2.0 which |
| 6 | * accompanies this distribution and is available at |
| 7 | * |
| 8 | * https://www.eclipse.org/legal/epl-v20.html |
| 9 | */ |
| 10 | |
| 11 | package org.junit.jupiter.api.function; |
| 12 | |
| 13 | import static org.apiguardian.api.API.Status.STABLE; |
| 14 | |
| 15 | import org.apiguardian.api.API; |
| 16 | |
| 17 | /** |
| 18 | * {@code ThrowingConsumer} is a functional interface that can be used to |
| 19 | * implement any generic block of code that consumes an argument and |
| 20 | * potentially throws a {@link Throwable}. |
| 21 | * |
| 22 | * <p>The {@code ThrowingConsumer} interface is similar to |
| 23 | * {@link java.util.function.Consumer}, except that a {@code ThrowingConsumer} |
| 24 | * can throw any kind of exception, including checked exceptions. |
| 25 | * |
| 26 | * <h2>Rationale for throwing {@code Throwable} instead of {@code Exception}</h2> |
| 27 | * |
| 28 | * <p>Although Java applications typically throw exceptions that are instances |
| 29 | * of {@link java.lang.Exception}, {@link java.lang.RuntimeException}, |
| 30 | * {@link java.lang.Error}, or {@link java.lang.AssertionError} (in testing |
| 31 | * scenarios), there may be use cases where a {@code ThrowingConsumer} needs to |
| 32 | * explicitly throw a {@code Throwable}. In order to support such specialized |
| 33 | * use cases, {@link #accept} is declared to throw {@code Throwable}. |
| 34 | * |
| 35 | * @param <T> the type of argument consumed |
| 36 | * @since 5.0 |
| 37 | * @see java.util.function.Consumer |
| 38 | * @see org.junit.jupiter.api.DynamicTest#stream |
| 39 | * @see Executable |
| 40 | * @see ThrowingSupplier |
| 41 | */ |
| 42 | @FunctionalInterface |
| 43 | @API(status = STABLE, since = "5.0") |
| 44 | public interface ThrowingConsumer<T> { |
| 45 | |
| 46 | /** |
| 47 | * Consume the supplied argument, potentially throwing an exception. |
| 48 | * |
| 49 | * @param t the argument to consume |
| 50 | */ |
| 51 | void accept(T t) throws Throwable; |
| 52 | |
| 53 | } |