Java学习(75)Java异常——throw & throws

1. 概述

(1) 可以通过throws声明将要抛出何种类型的异常,通过throw将产生的异常抛出;
(2) 如果一个方法可能会出现异常,但没有能力处理这种异常,可以在方法声明处用throws子句来声明抛出异常。

2. 结构

throws语句用在方法定义时声明该方法要抛出的异常类型。

public void method() throws Exception1,Exception2,...,ExceptionN{
	//可能产生异常的代码
}

3. 案例

(1) 方案1:throws后面接多个异常类型,中间用逗号分隔。

public class TryDemoThree {

	public static void main(String[] args) {
		try {
			int result = test();
			System.out.println("one和two的商是:" + result);
		} catch (ArithmeticException e) {
			System.out.println("除数不允许为零");
			e.printStackTrace();
		}catch(InputMismatchException e){
			System.out.println("请输入整数");
			e.printStackTrace();
		test();
		}
		
	public static int test() throws ArithmeticException,InputMismatchException{
	Scanner input = new Scanner(System.in);
	System.out.println("=====运算开始=====");
	System.out.print("请输入第一个整数:");
	int one = input.nextInt();
	System.out.print("请输入第二个整数:");
	int two = input.nextInt();
	System.out.println("=====运算结束=====");
	return one / two;
	}
}

注:
a. 通过选中可以放在try块里的代码右键Surround WithTry/catch Block快速新建try-catch模块。
b. 这里 test();不会报错,这是因为这两个异常都属于RuntimeException的子类,而RuntimeException属于非检查异常,编译器不会要求强制处理非检查异常,

(2) 方案2:throws后面接Exception。

public class TryDemoThree {
	public static void main(String[] args) {
		try{
			int result = test();
			System.out.println("one和two的商是:" + result);
		}catch(ArithmeticException e){
			
		}catch(InputMismatchException e){
			
		}catch(Exception e){
			
		}
		int result2=test();
	}

	public static int test() throws Exception{
	Scanner input = new Scanner(System.in);
	System.out.println("=====运算开始=====");
	System.out.print("请输入第一个整数:");
	int one = input.nextInt();
	System.out.print("请输入第二个整数:");
	int two = input.nextInt();
	System.out.println("=====运算结束=====");
	return one / two;
	}
}

注:
(1) 这里 test();会报错,但是 int result2=test();不会报错,这是因为含有检查异常,就会要求强制进行处理。
(2) 在类前面写/*...*/会自动产生文档供我们写异常提示

4. 使用throw抛出异常对象

(1) throw用来抛出一个异常。
例如:throw new IOException();
(2) throw抛出的只能够是可抛出类Throwable或者其子类的实例对象。
例如:throw new String(“出错啦”);是错误的。

5. throw结构

(1) 方案1

public void method(){
	try {
		// 代码段1
		throw new 异常类型();
	}catch(异常类型 ex) {
		// 对异常进行处理的代码段2
	}
}

(2) 方案2

public void method() throws 异常类型{
	// 代码段1
	throw new 异常类型();
}

(3) 作用

a. 规避可能出现的风险;
b. 完成一些程序的逻辑。

6. throw案例

描述酒店的入住规则:限定年龄,18岁以下,80岁以上的住客必须由亲友陪同

(1) 方案1:通过try…catch包含throw语句–自己抛自己处理

public class TryDemoFour {
	public static void main(String[] args) {
		testAge();
	/* throw抛出异常对象的处理方案:
	 * 1、通过try..catch包含throw语句--自己抛自己处理
	 * 2、通过throws在方法声明出抛出异常类型--谁调用谁处理--调用者可以自己处理,也可以继续上抛
	 *    此时可以抛出与throw对象相同的类型或者其父类
	 */
	// 描述酒店的入住规则:限定年龄,18岁以下,80岁以上的住客必须由亲友陪同
	public static void testAge() {

		try {
			System.out.println("请输入年龄:");
			Scanner input = new Scanner(System.in);
			int age = input.nextInt();
			if (age < 18 || age > 80) {
				throw new Exception("18岁以下,80岁以上的住客必须由亲友陪同");
			} else {
				System.out.println("欢迎入住本酒店");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

运行结果:

请输入年龄:
81
java.lang.Exception: 18岁以下,80岁以上的住客必须由亲友陪同
	at com.study.test.TryDemoFour.testAge(TryDemoFour.java:34)
	at com.study.test.TryDemoFour.main(TryDemoFour.java:9)

(2) 方案2:通过throws在方法声明出抛出异常类型

public class TryDemoFour {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
//		testAge();
		try {
			testAge();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/*
	 * throw抛出异常对象的处理方案:
	 * 2、通过throws在方法声明出抛出异常类型--谁调用谁处理--调用者可以自己处理,也可以继续上抛
	 *    此时可以抛出与throw对象相同的类型或者其父类
	 */
	// 描述酒店的入住规则:限定年龄,18岁以下,80岁以上的住客必须由亲友陪同
	public static void testAge() throws HotelAgeException {
		System.out.println("请输入年龄:");
		Scanner input = new Scanner(System.in);
		int age = input.nextInt();
		if (age < 18 || age > 80) {
			throw new Exception("18岁以下,80岁以上的住客必须由亲友陪同");
		} else {
			System.out.println("欢迎入住本酒店");
		}
	}
}

注:通过throws在方法声明出抛出异常类型–谁调用谁处理–调用者可以自己处理,也可以继续上抛。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值