小编Jon*_*tan的帖子

如何干净地处理stream/forEach中的“未报告的异常IOException”?

我正在尝试使用 Java Streams 写入文件。据我所知,只要将异常抛出,就不必捕获异常。然而编译器会抛出一个错误(在stream.foreach()...的行上)并说

错误:未报告的异常 IOException;必须被抓住或宣布被扔出

有人可以解释这是为什么吗?(我只对使用流的解决方案感兴趣)

public void save(String file, ArrayList<String> text) throws IOException {
    FileWriter fw = new FileWriter(file);
    text.stream().forEach(x->fw.write(x +"\n"));
    fw.close();
}
Run Code Online (Sandbox Code Playgroud)

Netbeans 建议这样做,但是有没有更短的方法呢?

public void save(String file, ArrayList<String> text) throws IOException {
    FileWriter fw = new FileWriter(file);
    text.stream().forEach(x->{
        try {
            fw.write(x +"\n");
        } catch (IOException ex) {
            ...
        }
    });
    fw.close();
}
Run Code Online (Sandbox Code Playgroud)

java compiler-errors java-8 java-stream

6
推荐指数
1
解决办法
2296
查看次数

可选链接运算符奇怪的行为

我在 TypeScript (v4.6.2) 中遇到一些奇怪的行为。为什么 (a) 有效但 (b) 和 (c) 无效?

const a: string[] | null = []
if (a?.length > 1) {
    console.log(1)
}

const b = [] as (string[] | null)
if (b?.length > 1) {
    console.log(1)
}

const c: string[] | null = [] as (string[] | null)
if (c?.length > 1) {
    console.log(1)
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

TypeScript 游乐场

typescript

2
推荐指数
1
解决办法
1882
查看次数

在 Vue 中使用 i18n.detectLanguage()

我正在阅读Moxilla 文档并找到了函数 i18n.detectLanguage()。该文档指出可以通过以下方式使用:

var detectingLanguages = browser.i18n.detectLanguage(
  text                  // string
)
Run Code Online (Sandbox Code Playgroud)

然而,当我尝试在我的 Vue 应用程序中使用上面的代码时,我只是得到变量未在browser.

如何使用除了 Safari 之外的所有浏览器都支持的内置 i18n API?

javascript vue.js

0
推荐指数
1
解决办法
843
查看次数