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 org.checkerframework.checker.nullness.qual.Nullable;
019
020/**
021 * {@link Error} variant of {@link java.util.concurrent.ExecutionException}. As with {@code
022 * ExecutionException}, the error's {@linkplain #getCause() cause} comes from a failed task,
023 * possibly run in another thread. That cause should itself be an {@code Error}; if not, use {@code
024 * ExecutionException} or {@link UncheckedExecutionException}. This allows the client code to
025 * continue to distinguish between exceptions and errors, even when they come from other threads.
026 *
027 * @author Chris Povirk
028 * @since 10.0
029 */
030@GwtCompatible
031public class ExecutionError extends Error {
032  /*
033   * Ideally, this class would have exposed only constructors that require a non-null cause. See
034   * https://github.com/jspecify/jspecify-reference-checker/blob/61aafa4ae52594830cfc2d61c8b113009dbdb045/src/main/java/com/google/jspecify/nullness/NullSpecTransfer.java#L789
035   * and https://github.com/jspecify/jspecify/issues/490.
036   *
037   * (That would also have ensured that its cause was always an Error, rather than possibly another
038   * kind of Throwable that was later passed to initCause. Then we could have declared the override
039   * `public final Error getCause()`.)
040   */
041
042  /**
043   * Creates a new instance with {@code null} as its detail message and no cause.
044   *
045   * @deprecated Prefer {@linkplain ExecutionError(Error)} a constructor that accepts a cause: Users
046   *     of this class typically expect for instances to have a non-null cause. At the moment, you
047   *     can <i>usually</i> still preserve behavior by passing an explicit {@code null} cause. Note,
048   *     however, that passing an explicit {@code null} cause prevents anyone from calling {@link
049   *     #initCause} later, so it is not quite equivalent to using a constructor that omits the
050   *     cause.
051   */
052  @Deprecated
053  protected ExecutionError() {}
054
055  /**
056   * Creates a new instance with the given detail message and no cause.
057   *
058   * @deprecated Prefer {@linkplain ExecutionError(String, Error)} a constructor that accepts a
059   *     cause: Users of this class typically expect for instances to have a non-null cause. At the
060   *     moment, you can <i>usually</i> still preserve behavior by passing an explicit {@code null}
061   *     cause. Note, however, that passing an explicit {@code null} cause prevents anyone from
062   *     calling {@link #initCause} later, so it is not quite equivalent to using a constructor that
063   *     omits the cause.
064   */
065  @Deprecated
066  protected ExecutionError(@Nullable String message) {
067    super(message);
068  }
069
070  /**
071   * Creates a new instance with the given detail message and cause. Prefer to provide a
072   * non-nullable {@code cause}, as many users expect to find one.
073   */
074  public ExecutionError(@Nullable String message, @Nullable Error cause) {
075    super(message, cause);
076  }
077
078  /**
079   * Creates a new instance with {@code null} as its detail message and the given cause. Prefer to
080   * provide a non-nullable {@code cause}, as many users expect to find one.
081   */
082  public ExecutionError(@Nullable Error cause) {
083    super(cause);
084  }
085
086  private static final long serialVersionUID = 0;
087}