001/*
002 * Copyright (C) 2011 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.util.concurrent;
016
017import com.google.common.annotations.GwtCompatible;
018import javax.annotation.CheckForNull;
019
020/**
021 * Unchecked variant of {@link java.util.concurrent.ExecutionException}. As with {@code
022 * ExecutionException}, the exception's {@linkplain #getCause() cause} comes from a failed task,
023 * possibly run in another thread.
024 *
025 * <p>{@code UncheckedExecutionException} is intended as an alternative to {@code
026 * ExecutionException} when the exception thrown by a task is an unchecked exception. However, it
027 * may also wrap a checked exception in some cases.
028 *
029 * <p>When wrapping an {@code Error} from another thread, prefer {@link ExecutionError}. When
030 * wrapping a checked exception, prefer {@code ExecutionException}.
031 *
032 * @author Charles Fry
033 * @since 10.0
034 */
035@GwtCompatible
036@ElementTypesAreNonnullByDefault
037public class UncheckedExecutionException extends RuntimeException {
038  /*
039   * Ideally, this class would have exposed only constructors that require a non-null cause. See
040   * https://github.com/jspecify/jspecify-reference-checker/blob/61aafa4ae52594830cfc2d61c8b113009dbdb045/src/main/java/com/google/jspecify/nullness/NullSpecTransfer.java#L789
041   * and https://github.com/jspecify/jspecify/issues/490.
042   *
043   * (Perhaps it should also have required that its cause was a RuntimeException. However, that
044   * would have required that we throw a different kind of exception for wrapping *checked*
045   * exceptions in methods like Futures.getUnchecked and LoadingCache.get.)
046   */
047
048  /**
049   * Creates a new instance with {@code null} as its detail message and no cause.
050   *
051   * @deprecated Prefer {@linkplain UncheckedExecutionException(Throwable)} a constructor that
052   *     accepts a cause: Users of this class typically expect for instances to have a non-null
053   *     cause. At the moment, you can <i>usually</i> still preserve behavior by passing an explicit
054   *     {@code null} cause. Note, however, that passing an explicit {@code null} cause prevents
055   *     anyone from calling {@link #initCause} later, so it is not quite equivalent to using a
056   *     constructor that omits the cause.
057   */
058  @Deprecated
059  protected UncheckedExecutionException() {}
060
061  /**
062   * Creates a new instance with the given detail message and no cause.
063   *
064   * @deprecated Prefer {@linkplain UncheckedExecutionException(String, Throwable)} a constructor
065   *     that accepts a cause: Users of this class typically expect for instances to have a non-null
066   *     cause. At the moment, you can <i>usually</i> still preserve behavior by passing an explicit
067   *     {@code null} cause. Note, however, that passing an explicit {@code null} cause prevents
068   *     anyone from calling {@link #initCause} later, so it is not quite equivalent to using a
069   *     constructor that omits the cause.
070   */
071  @Deprecated
072  protected UncheckedExecutionException(@CheckForNull String message) {
073    super(message);
074  }
075
076  /**
077   * Creates a new instance with the given detail message and cause. Prefer to provide a
078   * non-nullable {@code cause}, as many users expect to find one.
079   */
080  public UncheckedExecutionException(@CheckForNull String message, @CheckForNull Throwable cause) {
081    super(message, cause);
082  }
083
084  /**
085   * Creates a new instance with {@code null} as its detail message and the given cause. Prefer to
086   * provide a non-nullable {@code cause}, as many users expect to find one.
087   */
088  public UncheckedExecutionException(@CheckForNull Throwable cause) {
089    super(cause);
090  }
091
092  private static final long serialVersionUID = 0;
093}