| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.exceptions; |
| 15 |
|
| 16 |
import io.reactivex.rxjava3.annotations.NonNull; |
| 17 |
import io.reactivex.rxjava3.internal.util.ExceptionHelper; |
| 18 |
|
| 19 |
/** |
| 20 |
* Utility class to help propagate checked exceptions and rethrow exceptions |
| 21 |
* designated as fatal. |
| 22 |
*/ |
| 23 |
public final class Exceptions { |
| 24 |
|
| 25 |
/** Utility class. */ |
| 26 |
private Exceptions() { |
| 27 |
throw new IllegalStateException("No instances!"); |
| 28 |
} |
| 29 |
/** |
| 30 |
* Convenience method to throw a {@code RuntimeException} and {@code Error} directly |
| 31 |
* or wrap any other exception type into a {@code RuntimeException}. |
| 32 |
* @param t the exception to throw directly or wrapped |
| 33 |
* @return because {@code propagate} itself throws an exception or error, this is a sort of phantom return |
| 34 |
* value; {@code propagate} does not actually return anything |
| 35 |
*/ |
| 36 |
@NonNull |
| 37 |
public static RuntimeException propagate(@NonNull Throwable t) { |
| 38 |
/* |
| 39 |
* The return type of RuntimeException is a trick for code to be like this: |
| 40 |
* |
| 41 |
* throw Exceptions.propagate(e); |
| 42 |
* |
| 43 |
* Even though nothing will return and throw via that 'throw', it allows the code to look like it |
| 44 |
* so it's easy to read and understand that it will always result in a throw. |
| 45 |
*/ |
| 46 |
throw ExceptionHelper.wrapOrThrow(t); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Throws a particular {@code Throwable} only if it belongs to a set of "fatal" error varieties. These |
| 51 |
* varieties are as follows: |
| 52 |
* <ul> |
| 53 |
* <li>{@code VirtualMachineError}</li> |
| 54 |
* <li>{@code ThreadDeath}</li> |
| 55 |
* <li>{@code LinkageError}</li> |
| 56 |
* </ul> |
| 57 |
* This can be useful if you are writing an operator that calls user-supplied code, and you want to |
| 58 |
* notify subscribers of errors encountered in that code by calling their {@code onError} methods, but only |
| 59 |
* if the errors are not so catastrophic that such a call would be futile, in which case you simply want to |
| 60 |
* rethrow the error. |
| 61 |
* |
| 62 |
* @param t |
| 63 |
* the {@code Throwable} to test and perhaps throw |
| 64 |
* @see <a href="https://github.com/ReactiveX/RxJava/issues/748#issuecomment-32471495">RxJava: StackOverflowError is swallowed (Issue #748)</a> |
| 65 |
*/ |
| 66 |
public static void throwIfFatal(@NonNull Throwable t) { |
| 67 |
// values here derived from https://github.com/ReactiveX/RxJava/issues/748#issuecomment-32471495 |
| 68 |
if (t instanceof VirtualMachineError) { |
| 69 |
throw (VirtualMachineError) t; |
| 70 |
} else if (t instanceof ThreadDeath) { |
| 71 |
throw (ThreadDeath) t; |
| 72 |
} else if (t instanceof LinkageError) { |
| 73 |
throw (LinkageError) t; |
| 74 |
} |
| 75 |
} |
| 76 |
} |