Java “抛出新异常”和“新异常”之间的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40200662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:29:32  来源:igfitidea点击:

Difference between "throw new Exception" and "new Exception"?

javaexceptionthrow

提问by Vineet Setia

I am interested to know best practice to use throw new Exception()and new Exception(). In case of using new Exception(), I have seen that code moves to next statement instead of throwing exception.

我有兴趣了解使用throw new Exception()和 的最佳实践new Exception()。在使用的情况下new Exception(),我已经看到代码移动到下一个语句而不是抛出异常。

But I am told that we should use new Exception()to throw RuntimeException.

但是我被告知我们应该使用new Exception()to throw RuntimeException

Can anyone throw some light on this ?

任何人都可以对此有所了解吗?

采纳答案by ΦXoc? ? Пepeúpa ツ

new Exception()means create an instance (same as creating new Integer(...)) but no exception will happen until you throw it...

new Exception()意味着创建一个实例(与创建 new Integer(...) 相同)但在你抛出它之前不会发生异常......

Consider following snippet:

考虑以下片段:

public static void main(String[] args) throws Exception {
    foo(1);
    foo2(1);
    }

    private static void foo2(final int number) throws Exception {
    Exception ex;
    if (number < 0) {
        ex = new Exception("No negative number please!");
        // throw ex; //nothing happens until you throw it
    }

    }

    private static void foo(final int number) throws Exception {
    if (number < 0) {
        throw new Exception("No negative number please!");
    }

    }

the method foo() will THROW an exception if the parameter is negative but the method foo2() will create an instance of exception if the parameter is negative

如果参数为负,方法 foo() 将抛出异常,但如果参数为负,方法 foo2() 将创建异常实例

回答by Daniel Scott

Exception e = new Exception ();

Just creates a new Exception, which you could later throw. Using

只需创建一个新的异常,您可以稍后抛出。使用

throw e;

Whereas

然而

throw new Exception()

Creates and throws the exception in one line

在一行中创建并抛出异常

To create and throw a runtime exception

创建并抛出运行时异常

throw new RuntimeException()

回答by SMA

new Exception()means you are creating a new instance of Exception type. Which means you are just instantiating an object similar to others like new String("abc"). You would do this when you are about to throw an exception of type Exceptionin next few lines of code execution.

new Exception()意味着您正在创建 Exception 类型的新实例。这意味着您只是在实例化一个类似于new String("abc"). 当您将Exception在接下来的几行代码执行中抛出类型异常时,您会这样做。

While when you say throw new Exception()this means you are saying move the program control to caller and don't execute the further statements after this throw statement.

而当你说throw new Exception()这意味着你是说将程序控制移到调用者并且不要在这个 throw 语句之后执行进一步的语句。

You would do this in a situation where you find that there is no way to move ahead and execute further and hence let caller know that i can't handle this case and if you know how to handle this case, please do so.

如果您发现无法继续前进并进一步执行,您会这样做,因此让调用者知道我无法处理这种情况,如果您知道如何处理这种情况,请这样做。

There is no best practice as such as you are comparing oranges with apples. But remember when throwing an exception, you always throw a meaningful exception like IO has where if file is not present it throws FileNotFoundExceptioninstead of its parent IOException.

没有最佳实践,例如您将橙子与苹果进行比较。但是请记住,在抛出异常时,您总是会抛出一个有意义的异常,例如 IO 有 where if file is not present 它抛出FileNotFoundException而不是它的 parent IOException