为什么要使用throw与throws
首先通过一段代码简要说明:
public class Student{//student类
private int age;
public void setAge(int age) {
if(age<0||age>15) {
System.out.println("ERROR AGE;0<=AHE<=15");
}else {
this.age=age;
}
}
public int getAge() {
return age;
}
}
import com.jd.vo.Student;//引包,引用com.jd.vo包中的Student类
public class Test {
public static void main(String[] args) {
Student student = new Student();
student.setAge(150);
int age = student.getAge();
System.out.println(age);
}
}
输出结果为:
ERROR AGE;0<=AHE<=15
0
此时单纯输出无法确定程序哪个地方出现问题,需要通过使用throw抛出来显示异常出现的类型与位置。 进行调整:
public class Student {
private int age;
public void setAge(int age) {
if(age<0||age>15) {
throw new NullPointerException("年龄不符合要求,必须0-15");
//异常类型为空指针异常,属于自定义异常,与代码中的异常并不相符
}catch(AgeException e) {
e.printStackTrace();
}
}
else {
this.age=age;
}
}
}
上例中使用的异常信息和提示信息不符,不能见名知意所以需要自定义一个异常类,使其能够满足见名知意的效果。该类继承自已有的异常如果自定义检查时异常继承检查时异常类,则自定义异常属于检查时异常;如果继承运行时异常类,则自定义异常属于运行时异常。
此时我们定义一个AgeException异常类。
package com.jd.AgeException;
public class AgeException extends RuntimeException{
//继承自运行时异常类,属于运行时异常。
public AgeException(String age) {
super(age);
}
}
相应的需要在Student类程序中引包:
public class Student {
private int age;
public void setAge(int age) {
if(age<0||age>15) {
throw new NullPointerException("年龄不符合要求,必须0-15");
//异常类型为空指针异常,属于自定义异常,与代码中的异常并不相符
}catch(AgeException e) {
e.printStackTrace();
}
}
else {
this.age=age;
}
}
}
结果为:
Exception in thread "main" java.lang.NullPointerException: 年龄不符合要求,必须0-15
at com.jd.test.Test1.setAge(Test1.java:11)
at com.jd.test.Test.main(Test.java:10)
以此来实现自定义异常对于程序中不同数据的异常处理与显示。
throw与throws的使用方法与区别
使用方法:
如果throw抛出的是由 运行时异常 创建的对象,则不需要显式使用throws;否则需要显式使用throws或try-catch-finally,即检查时异常需要使用throws或try-catch-finally显式处理。
区别:
1、抛出类型上:
throw抛出异常类创建的对象;
throws抛出异常类。
2、使用位置上:
throw:用于方法或代码块;
throws:用于方法参数列表的后面。
创建一个新的Test类代码进行举例说明:
public class Test {
public static void main(String[] args) throws Exception {
/* {//此种为使用try-catch -finally显式处理。
int a=100;
try {
throw new Exception("年龄无效");
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(a/b);
}*/
int a=120;
if(a>50||a<0) {
throw new Exception("年龄无效");
//System.out.println("1 "+a); //throw后面不允许直接跟代码
}
else{
System.out.println("2 "+a);
}
}
}
运行结果为:
Exception in thread "main" java.lang.Exception: 年龄无效
at com.baidu.test.Test.main(Test.java:22)
此时该异常为检查时异常,所以需要使用throws抛出或者try-catch-finally进行显式处理。
public class Test2 {
//只要异常没被try-catch-finally解决完,就会被抛出!
public static void div(int b) throws Exception {
if(b==0) {
try {
throw new Exception("分母不能为0");
} catch (Exception e) {
System.out.println("执行");
e.printStackTrace();
}
}else{
System.out.println(1/b);
}
}
public static void main(String[] args) {
try {
div(0);
} catch (Exception e) {
System.out.println("永远无法执行");
}
}
}
运行结果为:
执行
java.lang.Exception: 分母不能为0
at com.jd.test.Test2.div(Test2.java:8)
at com.jd.test.Test2.main(Test2.java:21)
public class Test2 {
//只要异常没被try-catch-finally解决完,就会被抛出!
public static void div(int b) throws Exception {
if(b==0) {
try {
throw new Exception("分母不能为0");
} catch (Exception e) {
System.out.println("执行");
e.printStackTrace();
}
}
System.out.println(1/b);//运行时出现异常,且未被try-catch-finally解决,所以抛出从而输出进入main方法中被其解决。
}
public static void main(String[] args) {
try {
div(0);
} catch (Exception e) {
System.out.println("永远无法执行");
}
}
}
运行结果为:
执行
java.lang.Exception: 分母不能为0
at com.jd.test.Test2.div(Test2.java:8)
at com.jd.test.Test2.main(Test2.java:21)
永远无法执行
出现上下两端代码执行结果不同的原因为:是否执行异常语句System.out.println(1/b);,可解释为: 语句运行时出现异常,且未被try-catch-finally解决,所以抛出从而输出进入main方法中被其解决。因此出现了执行main方法的现象。