首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编译JDK 11失败,JDK 8编译良好。

编译JDK 11失败,JDK 8编译良好。
EN

Stack Overflow用户
提问于 2019-06-26 10:36:10
回答 1查看 524关注 0票数 6

代码使用JDK 8 (1.8.0_212)编译良好,但未能使用JDK 11 (11.0.3)编译--同时使用Oracle和open (aws )

尝试使用javac编译,并使用Maven (maven版本3.6.1和maven-编译器-插件版本3.8.0)编译JDK 8,JDK 11失败。

代码语言:javascript
复制
import java.net.URL;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Function;
import java.util.stream.Stream;

public class AppDemo {
    public static void main(String[] args) {
        // NO error here
        giveMeStream("http://foo.com").map(wrap(url -> new URL(url)));

        List<String> list = new ArrayList<String>();
        list.add("http://foo.com/, http://bar.com/");
        // error: unreported exception MalformedURLException;
        // must be caught or declared to be thrown
        list.stream().flatMap(
            urls -> Arrays.<String>stream(urls.split(",")).map(wrap(url -> new URL(url)))
        );

        // error: unreported exception MalformedURLException;
        // must be caught or declared to be thrown
        Stream.concat(
            giveMeStream("http://foo.com").map(wrap(url -> new URL(url))),
            giveMeStream("http://bar.com").map(wrap(url -> new URL(url))));

    }


    static Stream<String> giveMeStream(String s) {
        return Arrays.stream(new String[]{s});
    }

    static <T, R, E extends Throwable> Function<T, R>
    wrap(FunException<T, R, E> fn) {
        return t -> {
            try {
                return fn.apply(t);
            } catch (Throwable throwable) {
                throw new RuntimeException(throwable);
            }
        };
    }

    interface FunException<T, R, E extends Throwable> {
        R apply(T t) throws E;
    }
}

错误:

代码语言:javascript
复制
Expected : No compilation error
Actual : compilation error for JDK11
Error message with JDK 11:

s.<String>stream(urls.split(",")).map(wrap(url -> new URL(url)))
                                                               ^
AppDemo.java:24: error: unreported exception MalformedURLException; must be caught or declared to be thrown
            giveMeStream("http://foo.com").map(wrap(url -> new URL(url))),
                                                           ^
AppDemo.java:25: error: unreported exception MalformedURLException; must be caught or declared to be thrown
            giveMeStream("http://bar.com").map(wrap(url -> new URL(url))));
                                                           ^
3 errors
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-26 11:29:45

因为对规范的轻微更新可能是这样说的。有关系吗?不会是这样的。

在这里将抛出的异常转换为参数化类型没有真正的目的。此外,您还可以使用这段代码构建RuntimeException链。试一试,而不是:

代码语言:javascript
复制
static <T, R> Function<T, R> wrap(FunException<T, R> fn) {
    return t -> {
        try {
            return fn.apply(t);
        } catch (Error | RuntimeException ex) {
            throw ex;
        } catch (Throwable throwable) {
            throw new RuntimeException("Checked exception in lambda", throwable);
        }
    };
}

interface FunException<T, R> {
    R apply(T t) throws Throwable;
}

现在它会编译得很好。

对读者说:不要这样做。处理java规则(例如检查异常)的正确方法是处理它们。使用黑客绕过一种语言的本质只意味着你的代码是非惯用的(其他读过你的代码的人不会理解它,你将很难阅读别人的代码。)这很糟糕),它倾向于以一种糟糕的方式与其他库交互,而各种应该有所帮助的特性现在受到了伤害(例如:这里有许多因果异常链,使得您的日志和异常跟踪的读取变得比所需的更困难)。此外,由于“偏离了老路”,这将导致一些有趣的时期,比如过去编译的代码不再编译。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56770799

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档