引入
封装引入:
看下面的代码:
package keeper;
public class Student {
public int age1 =89;
private int age =23;
public void setAge(int age ) {
if(age>29&&age<879) {
this.age=age;
}else {
System.out.println("年龄无效");
}
}
public int getAge( ) {
return age;
}
}
package keeper;
public class Test {
public static void main(String [] args) {
new Student().age1=5857324;
System.out.println(new Student().age1);
Student student =new Student();
student.setAge(852); //fuzhi
System.out.println(student.getAge());
}
}
结果:
89
852
因为age1变量为public级别的变量,任何人都可以使用,则定义Student类的人无法对该变量的范围进行有效的约束 。然而age变量则是private类型的。即:公共方法操作私有属性。
思考:System.out.println(“年龄无效”);即输出这种提示错误的方式对于调用该方法的程序员没有什么过多的直接的帮助。
解决方案:throw 异常对象
这样写虽然说也行,但是他报错报的是空指针异常,而我们想要他报错报的是年龄异常。给人一种词和意思不搭的别扭感。所以,我们要另外写一个年龄异常放在这里,然后当我们写错的时候,程序就会报年龄异常这个错误,而不是空指针异常。
所以,我们采用下面的方法:
自定异常:
为什么?
Java API提供的已有异常类无法准确表述当前发生的异常问题,这时就需要创建自定义的异常。
怎么做?
创建继承Exception 或其子类的自定义类;
自定义异常类调用父类构造函数(通常是含有一个String类型参数的构造函数);
例:
package keeper;
import cn.jd.vo.AgeException;
public class Student {
public int age1 =89;
private int age =23;
public void setAge(int age ) {
if(age>29&&age<879) {
this.age=age;
}else {
//System.out.println("年龄无效");
//throw new NullPointerException("年龄无效");
throw new AgeException("年龄无效");
}
}
public int getAge( ) {
return age;
}
}
package keeper;
public class Test {
public static void main(String [] args) {
new Student().age1=5857324;
System.out.println(new Student().age1);
Student student =new Student();
student.setAge(8); //赋值
System.out.println(student.getAge());
}
}
package cn.jd.vo;
public class AgeException extends RuntimeException{
private static final long serialVersionUID = 7652138953038774791L;
public AgeException(String message) {
super(message);
}
}
这样的话,对于另一个人而言,他就知道了,程序报错,什么错了呢?哦,年龄,在哪一行呢?然后就很清楚明白。
自定异常分类:
将自定异常抛给方法调用者:
package keeper;
import cn.jd.vo.AgeException;
public class Student {
public int age1 =89;
private int age =23;
public void setAge(int age ) {
if(age>29&&age<879) {
this.age=age;
}else {
throw new AgeException("年龄无效");
}
}
public int getAge( ) {
return age;
}
}
至于方法调用者是再抛给别人,还是抛给自己,那就不关我们的事情了。
将自定异常抛给自己:
package keeper;
import cn.jd.vo.AgeException;
public class Student {
public int age1 =89;
private int age =23;
public void setAge(int age ) {
if(age>29&&age<879) {
this.age=age;
}else {
try {
throw new AgeException("年龄无效");
}catch(AgeException e) {
e.printStackTrace();
}
}
}
public int getAge( ) {
return age;
}
}
throw
throw用于抛出异常对象;
1、如果异常对象为运行时,
则
方法参数列表后面可以不使用throws,不使用try-catch。
也可以将异常抛给方法调用者;
例:
package keeper;
import cn.jd.vo.AgeException;
public class Student {
public int age1 =89;
private int age =23;
public void setAge(int age ) {
if(age>29&&age<879) {
this.age=age;
}else {
throw new AgeException("年龄无效");
}
}
public int getAge( ) {
return age;
}
}
2、如果异常对象为检查时,
则
方法参数列表后面必须使用throws抛出创建该对象的类;
如果没有throws 必须用try-catch。
例:
用throws:
package keeper;
import cn.jd.vo.AgeException;
public class Student {
public int age1 =89;
private int age =23;
public void setAge(int age ) throws AgeException {
if(age>29&&age<879) {
this.age=age;
}else {
throw new AgeException("年龄无效");
}
}
public int getAge( ) {
return age;
}
}
用try-catch:
public class Student {
public int age1 =89;
private int age =23;
public void setAge(int age ) {
if(age>29&&age<879) {
this.age=age;
}else {
try {
throw new AgeException("年龄无效");
} catch (AgeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public int getAge( ) {
return age;
}
}
throws
区别:
抛出的东西不同:
throw抛出的是具体的异常对象,
而
throws抛出的是抽象的异常类;
使用位置不同:
throw一般用在方法体中,也可用在代码块中,但是如果抛出的是检查时异常类创建的对象,则必须使用try-catch自行处理;
throws只能用在方法声明括号后面;
throw用在代码块中:
{
int a=12;
if(a==12) {
throw new AgeException("");
}
}