详述throw与throws

引入

封装引入:
看下面的代码:

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(""); 
		}
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值