详述throw和throws,运行时异常和检查时异常

运行时异常:

这里自定义了AgeException类继承运行时异常类

public class AgeException extends RuntimeException{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3587901812576863406L;

	public AgeException(String arg0) {
		super(arg0);	
	}
}
public class Student {

	private int age;
	public void setAge(int age) {//3.public void setAge(int age) throws AgeException抛给了方法调用者
		if (age>1 && age<20) {
			this.age = age;
		}else {
			throw new AgeException("年龄无效");//2.执行else时隐式往上抛
		}
	}
	
	public static void main(String[] args) {//public static void main(String[] args) throws AgeException抛给了JVM
		Student stu = new Student();
//		stu.setAge(100);//1.从这里进入   //4.抛到此处然后分了两种,第一种隐式抛给上面
		try {			
			stu.setAge(100);//第二种自己解决
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

在这里解决的意思就是用异常对象打印出提示以及红色的异常信息
比如:
e.printStackTrace();
throw new UnsupportedOperationException(“第二个参数不能为0”);

向上抛自己解决

非运行时异常,检查时异常:

这里自定义了AgeException类继承检查时异常类

package com.jd;

public class AgeException extends Exception{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3587901812576863406L;
	
	public AgeException(String arg0) {
		super(arg0);
	}
}
public class Student {

	private int age;
	public void setAge(int age) {
		if (age>1 && age<20) {
			this.age = age;
		}else {
			throw new AgeException("年龄无效");//这是报错的,必须显式解决:往上抛或自己解决,
							//自己解决的话调用者就没事了,无论怎么做必须二者选一
		}
	}
	
	public static void main(String[] args) {
		Student stu = new Student();
		stu.setAge(100);
	}
}

正确的两种做法

public class Student {

	private int age;
	public void setAge(int age) {
		if (age>1 && age<20) {
			this.age = age;
		}else {
			try {
				throw new AgeException("年龄无效");//①try-catch自我解决
			} catch (AgeException e) {
				e.printStackTrace();
			}
		}
	}
	public static void main(String[] args) {
		Student stu = new Student();
		stu.setAge(100);//上面解决了这里就不用管了
	}
}
public class Student {

	private int age;
	public void setAge(int age) throws AgeException {
		if (age>1 && age<20) {
			this.age = age;
		}else {
				throw new AgeException("年龄无效");//②往上抛
		}
	}
	public static void main(String[] args) {
		Student stu = new Student();
		stu.setAge(100);//也是两种选择必选其一,往上抛给AgeException进而交给JVM,
		//或自己解决。。。以下两种选一个就好
	}
//①	public static void main(String[] args) throws AgeException {//交给了JVM
//		Student stu = new Student();
//		stu.setAge(100);
//	}
//}	

//②	public static void main(String[] args) {
//		Student stu = new Student();
//		try {
//			stu.setAge(100);
//		} catch (AgeException e) {//自己解决了
//			e.printStackTrace();
//		}
//	}
}

总结:

异常对象和调用者都有两种选择:往上抛和自己解决,自己解决了不用下一级管了,就结束了。
异常对象往上抛是抛给调用者的,调用者往上抛是交给JVM的。
运行时异常是隐式向上抛的,非运行时异常不会隐式向上抛故必须显式指定怎样去解决,解决的意思就是用异常对象打印出提示以及红色的异常信息

配套的代码

package com.jd;
public class Student {

//	public int age;//如果该变量为public级别的变量,则定义Student类的人无法对该变量的范围进行有效的约束   公共方法操作私有属性
	
	private int age;
//	public void setAge(int age) {
//		if (age>1 && age<20) {
//			this.age = age;
//		}else {
//			//System.out.println("年龄无效");//思考:作用不大,解决方案抛出异常可以准确定位到代码出错的位置。throw 异常对象 
//			//throw new NullPointerException("年龄无效");//思考:词不达意 
//			throw new AgeException("年龄无效");//如何自定义异常:创建继承Exception 或其子类的自定义类;
//			自定义异常类调用父类构造函数(通常是含有一个String类型参数的构造函数);
//		}
//	}
	
	
//	public void setAge(int age) {
//		if (age>1 && age<20) {
//			this.age = age;
//		}else {
//			try {
//				throw new AgeException("年龄无效");//自己解决
//			}catch(AgeException e) {
//				e.printStackTrace();
//			}
//			
//			//throw new AgeException("年龄无效");//将异常抛给方法调用者
//		}
//	}
	
	public void setAge( int age){
		if (age>1 && age<20) {
			this.age = age;
		}else {
			throw new AgeException("年龄无效");
			/*throw用于抛出异常对象;
			 * 1、如果异常对象为运行时,则方法参数列表后面可以不使用throws,也可以将异常抛给方法调用者;try-catch
			 * 2、如果异常对象为检查时,则方法参数列表后面使用throws抛出创建该对象的类;或try-catch
			 * 
			 * throw与throws的区别:
				抛出的东西不同:throw抛出的是具体的异常对象,而throws抛出的是抽象的异常类;
				使用位置不同:throw一般用在方法体中,也可用在代码块中,但是如果抛出的是检查时异常类创建的对象,则必须使用try-catch自行处理;
							throws只能用在方法声明括号后面;
			 */
		}
	}
	
	public int getAge(){
		return age;
	}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值