简体   繁体   English

异常处理:Java 中 throw 与 throws 的区别

[英]Exception handling : Difference between throw vs throws in Java

Could someone explain me when it is useful to use the keyword throw new .. instead of using throws next to the signature of a method ?有人可以解释我何时使用关键字 throw new .. 而不是在方法签名旁边使用 throws 有用吗?

I know that when a method throws a Checked Exception.我知道当一个方法抛出一个检查异常时。 Java forces us to deal with it by doing it directly in the method by handling the Exception into a try-catch bloc or by specifying that it will be done elsewhere with the keyword throws next to the signature. Java 强制我们通过直接在方法中处理它来处理它,方法是将异常处理到 try-catch 块中,或者通过在签名旁边使用关键字 throws 指定它将在其他地方完成。

However, I have some trouble to understand when it is useful to use the keyword throw new and why.但是,我很难理解何时使用关键字 throw new 以及为什么有用。 Is it related with handling Unchecked Exceptions ?它与处理未经检查的异常有关吗?

For instance in this example.例如在这个例子中。 Why don't we throw new ArithmeticException() in the method compute ?为什么我们不在方法 compute 中抛出 new ArithmeticException() ? Since ArithmeticException is an unchecked one ?因为 ArithmeticException 是未经检查的? Should we not add something like :我们是否应该添加类似的内容:

private static int compute(int i) { 
    if(i == 0) {
        throw new ArithmeticException();
    }
    return 1/i; 
}

. .

public class Main {
  public static void main(String[] args) {
     for (int i = -2; i < 2; i++) { 
        try { 
           System.out.println(i+" -> "+compute(i)); 
        
        } catch (ArithmeticException e) {
             System.out.println(i+" -> undefined")
        }
     }
  }

   
 private static int compute(int i) { 
      return 1/i; 
  }

} }

throws tells others that this method can throw an exception. throws告诉其他人这个方法可以抛出异常。 Think of it as documentation.将其视为文档。 Checked exceptions must be part of a method's signature.检查的异常必须是方法签名的一部分。

throw actually throws the exception. throw实际上会抛出异常。 ( throw new Exception(); first creates a new exception instance and then throws that instance. You could use two separate statements: Exception ex = new Exception(); throw ex; ) You can throw both checked and unchecked exceptions with the throw keyword. throw new Exception();首先创建一个新的异常实例,然后抛出该实例。您可以使用两个单独的语句: Exception ex = new Exception(); throw ex; )您可以使用throw关键字同时抛出已检查和未检查的异常.

void checked() throws Exception { // required, Exception is a checked exception
  throw new Exception();
}

void unchecked() {
  throw new RuntimeException(); // throws not required, RuntimeException is not checked
}

void checkedCall() throws Exception {
  checked(); // throws required, because "checked" can throw a checked exception
}

void caught() {
  try {
    checked();
  } catch (final Exception ex) {
    // throws is no longer required, because the (checked) exception is handled within the method itself. It never leaves the method.
  }
}

Regarding your updated question: you don't throw new ArithmeticException , because it is thrown by the JVM when it tries to apply the / operator.关于您更新的问题:您不会throw new ArithmeticException ,因为它是由 JVM 在尝试应用/运算符时抛出的。 You can throw it manually if you want to, but that's just redundant code.如果你愿意,你可以手动抛出它,但这只是多余的代码。

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

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