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

运行时异常(RuntimeException)

定义:运行时异常,即程序运行时抛出的异常。这种异常在写代码时不进行处理,Java源文件也能编译通过。 RuntimeException异常类及其下面的子类均为运行时异常。
注意;运行时异常会直接或间接继承RuntimeException,通常不需要显式throws异常类处理

package com.jd.test2;
//自定义异常类
public class AgeException extends RuntimeException{

	public AgeException (String msg) {
		super(msg);
	};
	
}
package com.jd.test1;
//这里自定义了AgeException类继承运行时异常类
import com.jd.test2.AgeException;

//运行时异常(RuntimeException)

public class Test1 {
//运行时异常可以直接throw抛出异常,即打印异常提示信息,不需要显式throws处理,但此时抛出代码后,下面代码不会运行
//throw异常抛出一般写在方法体中或代码块中
//也可以自己解决,使用try-catch-finally方法,解决之后会打印红字异常提示信息,但是和throw相比,下面代码会继续运行
	private int age;
	public void setAge(int age){
		if(age<0 || age>150) {
			throw new AgeException("年龄大于零岁小于150岁");
		}else {
			System.out.println(age);
		}
	}
	public static void main(String[] args) {
		Test1 test1 = new Test1();

			test1.setAge(160);
		
		System.out.println("晚安");
	}
}

在这里插入图片描述

package com.jd.test1;
//这里自定义了AgeException类继承运行时异常类
import com.jd.test2.AgeException;

//运行时异常(RuntimeException)

public class Test1 {
//运行时异常可以直接throw抛出异常,即打印异常提示信息,不需要显式throws处理,但此时抛出代码后,下面代码不会运行
//throw异常抛出一般写在方法体中或代码块中
//也可以自己解决,使用try-catch-finally方法,解决之后会打印红字异常提示信息,但是和throw相比,下面代码会继续运行
	private int age;
	public void setAge(int age){
		if(age<0 || age>150) {
			throw new AgeException("年龄大于零岁小于150岁");
		}else {
			System.out.println(age);
		}
	}
	public static void main(String[] args) {
		Test1 test1 = new Test1();
		try {
			test1.setAge(160);
		} catch (AgeException e) {
			e.printStackTrace();
		}	
		System.out.println("晚安");
	}
}

在这里插入图片描述

检查时异常(CheckedException)

定义:检查时异常,又称为非运行时异常,这样的异常必须在编程时进行处理,否则就会编译不通过。Exception异常类及其子类(除去RuntimeException异常类及其子类)都是检查时异常。

package com.jd.test2;
//这里自定义AgeException为检查时异常类
public class Test2 {
//检查时异常不能直接throw抛出异常对象,需要显式处理
	
	private int age;
	public void setAge(int age) throws AgeException  {//这里异常抛给方法调用者
		if(age<0 || age>150) {
			throw new AgeException("年龄大于零岁小于150岁");//这里异常隐式向上抛
		}else {
			System.out.println(age);
		}
	}
	public static void main(String[] args) throws AgeException {//这里异常抛给JVM
		Test2 test2 = new Test2();
		test2.setAge(160);
		System.out.println("晚安");
	}
}

注意:异常抛出后线面代码不会执行
在这里插入图片描述

package com.jd.test2;
//这里自定义AgeException为检查时异常类
public class Test2 {
//检查时异常不能直接throw抛出异常对象,需要显式处理
//也可以使用try-catch-finally方法处理
	private int age;
	public void setAge(int age){
		if(age<0 || age>150) {
		//try-catch-finally方法处理,异常被打印出类,下面代码继续运行
			try {
				throw new AgeException("年龄大于零岁小于150岁");
			} catch (AgeException e) {
				e.printStackTrace();
			}
		}else {
			System.out.println(age);
		}
	}
	public static void main(String[] args) throws AgeException {
		Test2 test2 = new Test2();
		test2.setAge(160);
		System.out.println("晚安");
	}
}

在这里插入图片描述

package com.jd.test2;
//这里自定义AgeException为检查时异常类
public class Test2 {
//检查时异常不能直接throw抛出异常对象,需要显式处理
//也可以使用try-catch-finally方法处理
	private int age;
	public void setAge(int age) throws AgeException{
		if(age<0 || age>150) {
				throw new AgeException("年龄大于零岁小于150岁");
		}else {
			System.out.println(age);
		}
	}
	public static void main(String[] args){
		Test2 test2 = new Test2();
		try {
			test2.setAge(160);
		} catch (AgeException e) {
			e.printStackTrace();
		}
		System.out.println("晚安");
	}
}

在这里插入图片描述
两种方法都可以解决问题

总结:

如果是运行时异常:运行时异常可以直接throw抛出异常,即打印异常提示信息,不需要显式throws处理,但此时抛出代码后,下面代码不会运行;
也可以自己解决,使用try-catch-finally方法,解决之后会打印红字异常提示信息,但是和throw相比,下面代码会继续运行
如果是检查时异常:检查时异常不能直接throw抛出异常对象,需要显式throws异常类处理,也可以使用try-catch-finally方法处理。同样的,throw抛出异常后代码停止,try-catch-finally解决后代码继续运行。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值