java - 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?
解决方案
Throwable
是 的超类Exception
,意味着catch (Throwable var)
也捕获异常。因此,vavr 中的代码是正确的——只要有任何Throwable
抛出,它都会被包裹在Try.Failure
.
推荐阅读
- python - 在 python 中使用基本递归绘制分形时出错
- c# - JsonConvertDeserializeObject 无法正常工作
- javascript - 如何从 BigInt 计算百分比?
- go - 合并具有一些重叠字段的结构
- c# - 如何解析INI文件?
- javascript - NativeScript Vue RadAutoCompleteTextView 以编程方式删除所有标记
- java - 如何从一个类中获取 int 值并在另一个类中使用它
- android-studio - 为什么我可以通过 android 模拟器运行一个简单的计数器应用程序,但不能通过 ios 模拟器运行?
- html - 如何仅在中间图像对齐?
- swift - SwiftUI .onTapGesture 未从 foreach 循环内部调用