java异常:使用throws声明异常类型

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
通过throws抛出异常时,针对可能出现的多种异常情况,有以下两种解决方案:
方案一:throws后面接多个异常类型,中间用逗号分隔

package java_exception;

import java.util.InputMismatchException;
import java.util.Scanner;

public class TryDemoThree {
    /**
     * 通过throws抛出异常时,针对可能出现的多种异常情况,解决方案:
     * 1.throws后面接多个异常类型,中间用逗号分隔
     */
    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;
    }
    public static void main(String[] args) {
        // TryDemoTwo t = new TryDemoTwo();
        try{
            int result = test();
            System.out.println("one和two的商是:" + result);
        }catch(ArithmeticException e){
            System.out.println("除数不能为0!");
            e.printStackTrace();
        }catch(InputMismatchException e){
            System.out.println("请输入整数!");
            e.printStackTrace();
        }catch(Exception e){

        }
    }
}

方案二:

package java_exception;

import java.util.InputMismatchException;
import java.util.Scanner;

public class TryDemoThree {
    /**
     * 通过throws抛出异常时,针对可能出现的多种异常情况,解决方案:
     * 1.throws后面接多个异常类型,中间用逗号分隔
     * 2.throws后面接Exception
     */
    /**
     * 测试接收数据相除结果的方法
     * @return 两个接收数据的商
     * @throws ArithmeticException
     * @throws InputMismatchException
     */
    // 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;
    // }
    
    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;
    }
    public static void main(String[] args) {
        // TryDemoTwo t = new TryDemoTwo();
        try{
            int result = test();
            System.out.println("one和two的商是:" + result);
        }catch(ArithmeticException e){
            System.out.println("除数不能为0!");
            e.printStackTrace();
        }catch(InputMismatchException e){
            System.out.println("请输入整数!");
            e.printStackTrace();
        }catch(Exception e){

        }
    }
}

当抛出的是Exception(异常的父类)时,在try-catch结构中可以不写具体会出现哪种类型的异常,但必须写Exception,即:

try{
	int result = test();
	System.out.println("one和two的商是:" + result);
}catch(Exception e){

}

是可以的。

但:

try{
	int result = test();
    System.out.println("one和two的商是:" + result);
}catch(ArithmeticException e){
    System.out.println("除数不能为0!");
    e.printStackTrace();
}catch(InputMismatchException e){
    System.out.println("请输入整数!");
    e.printStackTrace();
}

不可以。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值