为什么Java编译器拒绝将System.exit()识别为过程终止? [英] Why does Java compiler refuse to recognize System.exit() as a procedure termination?

查看:57
本文介绍了为什么Java编译器拒绝将System.exit()识别为过程终止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java的编译器(至少是我使用的Oracle的编译器)拒绝将System.exit()识别为过程终止.例如,以下代码给出了编译错误:

Java's compiler, at least the one from Oracle that I use, refuses to recognize System.exit() as a procedure termination. For example, the following code gives a compilation error:

public static int readInteger( ArrayList<String> listLines, int iLineNumber0 ){
    try {
        int value = Integer.parseInt( listLines.get( 0 ) );
        return value;
    } catch( Throwable t ) {
        System.err.println( "error reading line: " + iLineNumber0 + ": " + t );
        System.exit( -1 );
    }
}

错误是:缺少返回语句."因此,要完成这项工作,我必须添加一个这样的return语句(成功编译):

The error is: "Missing return statement." Therefore, to make this work I have to add in a return statement like this (compiles successfully):

public static int readInteger( ArrayList<String> listLines, int iLineNumber0 ){
    try {
        int value = Integer.parseInt( listLines.get( 0 ) );
        return value;
    } catch( Throwable t ) {
        System.err.println( "error reading line: " + iLineNumber0 + ": " + t );
        System.exit( -1 );
    }
    return 0; // unreachable code
}

具有讽刺意味的是,尽管编译器也没有意识到这一点,但最终的return语句是无法访问的代码.

Ironically, the final return statement that is needed is unreachable code, although the compiler does not realize this either.

推荐答案

Java的编译器(至少是我使用的Oracle编译器)拒绝将System.exit()识别为过程终止.

Java's compiler, at least the one from Oracle that I use, refuses to recognize System.exit() as a procedure termination.

是的,会的.就编译器而言,它只是一个 void 方法.没有办法在方法签名中指示此方法永远不会正常返回",并且在语言中也没有这样的概念.例如:

Yes, it would. As far as the compiler is concerned, it's just a void method. There's no way of indicating "this method never returns normally" in a method signature, and no such concept in the language. For example:

public void alwaysThrow()
{
    throw new RuntimeException();
}

...

alwaysThrow();
System.out.println("This line is never reached");

就编译器而言,即使是我们都知道它永远不会执行,但上述代码段中的最后一行仍然可以访问.同样,您的额外退货单在技术上是可以达到的,但在实践上是不可行的.

The last line in the above snippet is still reachable as far as the compiler is concerned, even though we know it will never execute. Likewise your extra return statement is technically reachable, but practically unreachable.

尽管据我所知,这是一种影响大多数语言的语言,但基本上可以认为这是一种语言缺陷.能够表示这样的方法会很不错,但这很少是现实生活中的一个现实问题.

Basically this could be deemed a flaw in the language, although it's one which affects most languages as far as I'm aware. While it would be nice to be able to represent methods like this, it's rarely a real issue in real life.

如果发现自己受其困扰,可以编写一个辅助方法:

If you find yourself bothered by it, you could write a helper method:

public RuntimeException systemExit(int exitValue)
{
    System.exit(exitValue);
    return new RuntimeException("Shouldn't get here");
}

然后将其称为:

throw systemExit();

这将确保就编译器而言,该语句的末尾不可访问,因此您可以:

That will ensure that the end of the statement is unreachable as far as the compiler is concerned, so you could have:

catch (Throwable t) {
    System.err.println("error reading line: " + iLineNumber0 + ": " + t);
    throw systemExit(-1);
}

...,您的编译器错误将消失.

... and your compiler error would go away.

请注意,在其他类似情况下,可达性不是我们可能想要的一切.例如:

Note that there are other similar situations where reachability isn't everything we might want. For example:

int foo() {
    int x = someValue();
    if (x > 10) {
       return 1;
    }
    if (x <= 10) {
       return 20;
    }
    // Is this reachable or not?
}

我们知道 x 的任何值都将大于10或小于等于10,所以最后一行是实际上是无法访问的,但是语言的规则并没有表达...因此,即使是聪明的编译器也无法在不违反语言规范的情况下将上述代码视为有效代码.

We know that any value of x will either be greater than 10 or less-than-or-equal-to 10, so the final line is practically unreachable but the rules of the language don't express that... so even a smart compiler can't actually treat the above code as valid without violating the language specification.

这篇关于为什么Java编译器拒绝将System.exit()识别为过程终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆