我如何声明一个函数参数来接受抛出的函数?

我在Kotlin中定义了一个函数:

fun convertExceptionToEmpty(requestFunc: () -> List<Widget>): Stream<Widget> {
    try {
        return requestFunc().stream()
    } catch (th: Throwable) {
        // Log the exception...
        return Stream.empty()
    }
}

我已经用这个签名定义了一个Java方法:

List<Widget> getStaticWidgets() throws IOException;

我试图像这样编写它们:

Stream<Widget> widgets = convertExceptionToEmpty(() ->  getStaticWidgets())

当我编译时,我得到这个错误:

错误:(ln,col)java:未报告的异常java.io.IOException; 必须被捕获或宣布被抛出

如何定义我的函数参数以接受抛出的函数?


问题是Java检查了异常,但Kotlin没有。 requestFunc参数type () -> List<Widget>将被映射到函数接口Function0 <List <Widget >>,但操作符调用不会在Kotlin代码中引发检查异常。

所以你不能在lambda表达式中调用getStaticWidgets() ,因为它会抛出一个IOException ,这是Java中检查的异常。

由于您同时控制Kotlin和Java代码,最简单的解决方案是将参数type () -> List<Widget>更改为Callable<List<Widget>> ,例如:

// change the parameter type to `Callable` ---v
fun convertExceptionToEmpty(requestFunc: Callable<List<Widget>>): Stream<Widget> {
    try {
        //                 v--- get the `List<Widget>` from `Callable`
        return requestFunc.call().stream()
    } catch (th: Throwable) {
        return Stream.empty()
    }
}

然后,您可以进一步在Java8中使用方法引用表达式,例如:

Stream<Widget> widgets = convertExceptionToEmpty(this::getStaticWidgets);

//OR if `getStaticWidgets` is static `T` is the class belong to
//                                               v
Stream<Widget> widgets = convertExceptionToEmpty(T::getStaticWidgets);

恐怕没有什么可以做的,但赶上那个例外:

 Stream<Integer> widgets = convertExceptionToEmpty(() -> {
        try {
            return getStaticWidgets();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    });
链接地址: http://www.djcxy.com/p/97077.html

上一篇: How do I declare a function parameter to accept functions that throw?

下一篇: vs code cannot find module '@angular/core' or any other modules