简体   繁体   English

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

[英]Difference between “throw new Exception” and “new Exception”?

I am interested to know best practice to use throw new Exception() and new Exception() . 我有兴趣知道使用throw new Exception()new Exception()最佳实践。 In case of using new Exception() , I have seen that code moves to next statement instead of throwing exception. 在使用new Exception()情况下,我已经看到代码移动到下一个语句而不是抛出异常。

But I am told that we should use new Exception() to throw RuntimeException . 但我被告知我们应该使用new Exception()来抛出RuntimeException

Can anyone throw some light on this ? 任何人都可以对此有所了解吗?

new Exception() means create an instance (same as creating new Integer(...)) but no exception will happen until you throw it... new Exception()意味着创建一个实例(与创建新的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()将创建异常实例

Exception e = new Exception ();

Just creates a new Exception, which you could later throw. 只需创建一个新的Exception,稍后可以抛出。 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()

new Exception() means you are creating a new instance of Exception type. new Exception()表示您正在创建Exception类型的新实例。 Which means you are just instantiating an object similar to others like new String("abc") . 这意味着您只是实例化一个类似于其他对象的对象,如new String("abc") You would do this when you are about to throw an exception of type Exception in next few lines of code execution. 当你要在接下来的几行代码执行中抛出异常类型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 FileNotFoundException instead of its parent IOException . 但是请记住,在抛出异常时,你总是抛出一个有意义的异常,比如IO,如果文件不存在,它会抛出FileNotFoundException而不是它的父IOException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM