编译器不强制执行检查的异常处理

时间:2019-10-29 15:52:01

标签: java

我认为我在Java编译器中发现了一个错误。 在下面的代码中,功能测试抛出了一个已检查的异常。 当我在提交给执行者的Runnable内部使用函数并且while(true)内部使用该函数时,编译器未强制执行try / catch块或未向签名添加异常。 代码编译并成功运行。

这是编译器中的错误还是我缺少什么?

如果我删除while(true) 或不在提交中执行此操作,编译器将正常工作

编译:

 public static void main(String[] args) {
            Executors.newSingleThreadScheduledExecutor().submit(()-> {
                while (true) {
                    test();
                }
            });
        }

        public static void test() throws IOException {
            throw new IOException();
        }

未编译:

 public static void main(String[] args) {
            Executors.newSingleThreadScheduledExecutor().submit(()->{
                    test();      
            });
        }

也无法编译

   public static void main(String[] args) {
                while (true) {
                    test();   
            });
        }

预期:编译错误。 结果:构建成功

0 个答案:

没有答案
相关问题