1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 抛出异常关键字throw与定义异常关键字throws

抛出异常关键字throw与定义异常关键字throws

时间:2024-05-18 09:48:53

相关推荐

抛出异常关键字throw与定义异常关键字throws

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 thethrowandthrowskeywords 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 throwFileNotFoundExceptionbut the method declares to throw IOException. This is possible becauseIOExceptionis 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.

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。