| 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 Executable} is a functional interface that can be used to |
| 19 | * implement any generic block of code that potentially throws a |
| 20 | * {@link Throwable}. |
| 21 | * |
| 22 | * <p>The {@code Executable} interface is similar to {@link java.lang.Runnable}, |
| 23 | * except that an {@code Executable} can throw any kind of exception. |
| 24 | * |
| 25 | * <h2>Rationale for throwing {@code Throwable} instead of {@code Exception}</h2> |
| 26 | * |
| 27 | * <p>Although Java applications typically throw exceptions that are instances |
| 28 | * of {@link java.lang.Exception}, {@link java.lang.RuntimeException}, |
| 29 | * {@link java.lang.Error}, or {@link java.lang.AssertionError} (in testing |
| 30 | * scenarios), there may be use cases where an {@code Executable} needs to |
| 31 | * explicitly throw a {@code Throwable}. In order to support such specialized |
| 32 | * use cases, {@link #execute()} is declared to throw {@code Throwable}. |
| 33 | * |
| 34 | * @since 5.0 |
| 35 | * @see org.junit.jupiter.api.Assertions#assertAll(Executable...) |
| 36 | * @see org.junit.jupiter.api.Assertions#assertAll(String, Executable...) |
| 37 | * @see org.junit.jupiter.api.Assertions#assertThrows(Class, Executable) |
| 38 | * @see org.junit.jupiter.api.Assumptions#assumingThat(java.util.function.BooleanSupplier, Executable) |
| 39 | * @see org.junit.jupiter.api.DynamicTest#dynamicTest(String, Executable) |
| 40 | * @see ThrowingConsumer |
| 41 | * @see ThrowingSupplier |
| 42 | */ |
| 43 | @FunctionalInterface |
| 44 | @API(status = STABLE, since = "5.0") |
| 45 | public interface Executable { |
| 46 | |
| 47 | void execute() throws Throwable; |
| 48 | |
| 49 | } |