java使用throw和try-catch机制来处理异常。
In this Java tutorial, I’m going to tell you how to write code that declares to throw exceptions. This involves in using the throw
and throws
keywords together.
throw如何引发异常?
package ff;
public class Engineer {
protected int age;
public void setAge(int age) {
this.age = age;
}
}
我们要确保工程师的年龄始终在有效范围内,例如 在21到59之间。为此,我们可以检查参数age并在超出范围时引发异常。 因此,我们像下面一样更新setAge()方法:
package ff;
public class Engineer {
protected int age;
public void setAge(int age) {
if (age < 21 || age > 59) {
throw new IllegalArgumentException("Invalid age");
}
this.age = age;
}
}
Here, the throwkeyword is used to throw a new exception object if the passed-in parameter has invalid value. When the statement started by the throw keyword gets executed, the current method stops its execution and control returned to the caller code which can have an exception handler.
The following is a test program that uses try-catch structure to handle the above exception:
package Bean;
public class Engineer {
protected int age;
public void setAge(int age) {
if (age < 21 || age > 59) {
// 抛出一个异常
throw new IllegalArgumentException("Invalid age");
}
this.age = age;
}
public static void main(String[] args) {
try {
Engineer engineer = new Engineer();
engineer.setAge(12);
}
// 捕获异常
catch (IllegalArgumentException e) {
//输出控制台
System.out.println(e);
//错误控制台
System.err.println(e);
}
}
}
抛出一个异常:throw new IllegalArgumentException("Invalid age");
。
该程序尝试通过从命令行参数传递age
来创建一个新的Engineer
对象。 注意,在catch
块中,我们使用System.err
静态类打印出异常对象。 它代表错误控制台,它不同于System.out
静态类所代表的输出控制台。
上面的示例向您展示了如何使用throw
关键字在代码中引发异常
throws指定要在方法中引发的异常
我们如何知道方法是否可以抛出异常,如果可以抛出异常,那么它可以抛出哪些异常?
通过在方法签名中查找throws
子句,可以知道该方法是否可以抛出异常,以及可以抛出哪种异常?
如果调用使用throws关键字定义的方法,就必须处理调用该方法可能出现的异常
throws
关键字用于定义方法是否抛出异常
throw
用于抛出异常
example:
public int divide(int a, int b) throws Exception {
if (b == 0) {
throw new Exception("Cannot be devided by zero");
}
return a / b;
}
throw和throws的一些使用规则
Remember the following rules:
- If code in a method throws checked exceptions, the method must specify those exceptions in the throws clause.
- Unchecked exceptions are not required to be caught or declared.
- The exceptions specified in the throws clause can be broader than the ones can be thrown in the method’s body. Here’s an example:
public void deleteDir(String path) throws IOException {
if(path==null)
{
throw new FileNotFoundException();
}
}
Here, code in the method can throw FileNotFoundException
but the method declares to throw IOException. This is possible because IOException
is the parent class of FileNotFoundException.
- A method can declare to throw exceptions although its code doesn’t throw any exceptions. This is possible because the code can throw exceptions when it involves in the future. Here’s an example:
public void encryptFile(String path) throws IOException {
// no exceptions thrown here...
}
- Constructors can throw exceptions and declare to throw exceptions just like normal methods.
- We can also specify exceptions can be thrown by methods in an interface.