首页 > 解决方案 > Why does vavr's Try container catches Throwable but not Exception?

问题描述

I'm not an expert in java's type system and exception handling. But i found in SO that we should only catch exceptions but not throwable's.

Here is the link: Difference between using Throwable and Exception in a try catch

In Vavr's library i found this source code:

public interface Try<T> extends Value<T>, Serializable {
    long serialVersionUID = 1L;

    static <T> Try<T> of(CheckedFunction0<? extends T> supplier) {
        Objects.requireNonNull(supplier, "supplier is null");

        try {
            return new Try.Success(supplier.apply());
        } catch (Throwable var2) {
            return new Try.Failure(var2);
        }
    }
}

Would i have any issues in future if i will use this container? Will i miss some critical exceptions that may occur during execution of 'of' function?

标签: javaexceptionvavr

解决方案


Throwable是 的超类Exception,意味着catch (Throwable var)也捕获异常。因此,vavr 中的代码是正确的——只要有任何Throwable抛出,它都会被包裹在Try.Failure.


推荐阅读