Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从方法返回,在;试一试;块或在“之后”;捕获;块_Java_Exception_Try Catch - Fatal编程技术网

Java 从方法返回,在;试一试;块或在“之后”;捕获;块

Java 从方法返回,在;试一试;块或在“之后”;捕获;块,java,exception,try-catch,Java,Exception,Try Catch,以下两种方法有什么区别吗 哪一个更好?为什么 Prg1: Prg2: 不,这两种方法没有区别。 在这两种情况下,它都会有效地返回真值,只要处理了异常,就会恢复程序流。 只有在发生异常时才会访问Catch。没有区别,但第一个Prg1比Prg2快。请考虑不返回常量表达式的情况: 案例1: public static Val test() throws Exception { try { return doSomething(); } catch (Exception

以下两种方法有什么区别吗

哪一个更好?为什么

Prg1:

Prg2:


不,这两种方法没有区别。 在这两种情况下,它都会有效地返回真值,只要处理了异常,就会恢复程序流。
只有在发生异常时才会访问Catch。

没有区别,但第一个Prg1比Prg2快。

请考虑不返回常量表达式的情况:

案例1:

public static Val test() throws Exception {
    try {
        return doSomething();
    } catch (Exception e) {
        throw new Exception("No!");
    }
    // Unreachable code goes here
}
案例2:

public static Val test() throws Exception {
    Val toReturn = null;
    try {             
        toReturn = doSomething();
    } catch (Exception e) {
        throw new Exception("No!");
    }
    return toReturn;
}
我更喜欢第一个。第二个更冗长,在调试时可能会引起一些混乱

如果
test()

即使它只能返回
null
如果
doSomething
返回
null
。但这可能很难一目了然


然后你可以说,为了一致性起见,最好总是使用第一种形式。

我假设这是一个一般性问题。否则,我可能会对您的方法的其他方面进行评论

我认为在这种情况下,或者像这样的小方法,这并不重要。该方法足够简短,可以立即了解发生了什么,什么与什么相关,等等

但是,对于较长的方法,在第一个示例中,流程更容易遵循。在我看来。它将相关代码和相关场景放在一起。当您阅读该方法时,正常的执行流不会被
catch
块破坏,从而使其更加明显和“流畅”


但我不会将其推广到所有方法;这都是关于上下文的。

我更喜欢第二个片段,因为我觉得它更干净(更清晰)。我不认为这对性能有什么影响。我更喜欢第一个,因为如果您决定在本地处理异常而不是重新抛出异常,将会发生什么。有证据吗?通过使用此代码,您可以证明它:长开始时间、结束时间、持续时间;startTime=System.nanoTime();test2();//或者test1()endTime=System.nanoTime();首先,您正在更改程序。通过添加与代码内联的度量,您实际上正在影响代码的功能。您的程序现在必须运行不需要的步骤。这可能会改变编译器生成IL的方式
public static Val test() throws Exception {
    try {
        return doSomething();
    } catch (Exception e) {
        throw new Exception("No!");
    }
    // Unreachable code goes here
}
public static Val test() throws Exception {
    Val toReturn = null;
    try {             
        toReturn = doSomething();
    } catch (Exception e) {
        throw new Exception("No!");
    }
    return toReturn;
}
public static boolean test() throws Exception {
    try {
        doSomething();
        return true;
    } catch (Exception e) {
        throw new Exception("No!");
    }    
}