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 com.google.common.annotations.GwtIncompatible;
019import com.google.common.annotations.J2ktIncompatible;
020import org.jspecify.annotations.Nullable;
021
022/**
023 * Unchecked variant of {@link java.util.concurrent.ExecutionException}. As with {@code
024 * ExecutionException}, the exception's {@linkplain #getCause() cause} comes from a failed task,
025 * possibly run in another thread.
026 *
027 * <p>{@code UncheckedExecutionException} is intended as an alternative to {@code
028 * ExecutionException} when the exception thrown by a task is an unchecked exception. However, it
029 * may also wrap a checked exception in some cases.
030 *
031 * <p>When wrapping an {@code Error} from another thread, prefer {@link ExecutionError}. When
032 * wrapping a checked exception, prefer {@code ExecutionException}.
033 *
034 * @author Charles Fry
035 * @since 10.0
036 */
037@GwtCompatible
038public class UncheckedExecutionException extends RuntimeException {
039  /*
040   * Ideally, this class would have exposed only constructors that require a non-null cause. See
041   * https://github.com/jspecify/jspecify-reference-checker/blob/61aafa4ae52594830cfc2d61c8b113009dbdb045/src/main/java/com/google/jspecify/nullness/NullSpecTransfer.java#L789
042   * and https://github.com/jspecify/jspecify/issues/490.
043   *
044   * (Perhaps it should also have required that its cause was a RuntimeException. However, that
045   * would have required that we throw a different kind of exception for wrapping *checked*
046   * exceptions in methods like Futures.getUnchecked and LoadingCache.get.)
047   */
048
049  /**
050   * Creates a new instance with {@code null} as its detail message and no cause.
051   *
052   * @deprecated Prefer {@linkplain UncheckedExecutionException(Throwable)} a constructor that
053   *     accepts a cause: Users of this class typically expect for instances to have a non-null
054   *     cause. At the moment, you can <i>usually</i> still preserve behavior by passing an explicit
055   *     {@code null} cause. Note, however, that passing an explicit {@code null} cause prevents
056   *     anyone from calling {@link #initCause} later, so it is not quite equivalent to using a
057   *     constructor that omits the cause.
058   */
059  @Deprecated
060  protected UncheckedExecutionException() {}
061
062  /**
063   * Creates a new instance with the given detail message and no cause.
064   *
065   * @deprecated Prefer {@linkplain UncheckedExecutionException(String, Throwable)} a constructor
066   *     that accepts a cause: Users of this class typically expect for instances to have a non-null
067   *     cause. At the moment, you can <i>usually</i> still preserve behavior by passing an explicit
068   *     {@code null} cause. Note, however, that passing an explicit {@code null} cause prevents
069   *     anyone from calling {@link #initCause} later, so it is not quite equivalent to using a
070   *     constructor that omits the cause.
071   */
072  @SuppressWarnings("InlineMeSuggester") // b/387265535
073  @Deprecated
074  protected UncheckedExecutionException(@Nullable String message) {
075    super(message);
076  }
077
078  /**
079   * Creates a new instance with the given detail message and cause. Prefer to provide a
080   * non-nullable {@code cause}, as many users expect to find one.
081   */
082  public UncheckedExecutionException(@Nullable String message, @Nullable Throwable cause) {
083    super(message, cause);
084  }
085
086  /**
087   * Creates a new instance with {@code null} as its detail message and the given cause. Prefer to
088   * provide a non-nullable {@code cause}, as many users expect to find one.
089   */
090  public UncheckedExecutionException(@Nullable Throwable cause) {
091    super(cause);
092  }
093
094  @GwtIncompatible @J2ktIncompatible private static final long serialVersionUID = 0;
095}