本文介绍了throws x扩展了异常方法签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读,我碰到一个奇怪的方法签名;我从未见过我的生活:

Reading the JavaDoc of Optional, I bumped in a weird method signature; I never saw in my life:

public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier)
                                throws X extends Throwable

一瞥,我想知道如何通用异常< X扩展Throwable> 是可能的,因为你不能这样做(和)。第二个想法,这开始是有道理的,因为这里只是绑定供应商 ...但供应商本身在泛型之前就知道应该是什么类型。

At first glance, I wondered how the generic exception <X extends Throwable> is even possible, since you can't do that (here, and here). On second thought, this starts to make sense, as it is here just to bind the Supplier... but the supplier itself knows exactly what type it should be, before generics.

但是第二行打了我:


  • 抛出X 是一个完整的通用异常类型。

  • throws X is a complete generic exception type.

然后: