首页 > 其他 > 详细

抛出异常-throws和throw

时间:2019-02-12 23:42:35      阅读:262      评论:0      收藏:0      [点我收藏+]
package com.mpp.test;

import java.util.Scanner;

public class TryDemoFour {
    public static void main(String[] args) {
        try {
            testAge();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * throw抛出异常对象的处理方案
     * 1. 通过try.catch包含throw的语句--自己抛出自己处理
     * 2. 通过throws在方法声明处抛出异常类型--谁用谁处理--调用者可以自己处理,也可以继续向上抛
     * 3. 此时可以抛出与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) {
            e.printStackTrace();
        }
    }
*/

    public static void testAge() throws Exception {
            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:如果一个方法可能会出现异常,但没有能力处理这种异常,可以在方法声明处用throws子句来抛出异常类型。
throws 后面可以跟多个异常类型,用逗号分隔

技术分享图片

 


当方法OAO出异常时,方法不对异常做处理,而是调用该方法处做异常处理

技术分享图片

package com.mpp.test;

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

public class TryDemoThree {
    public static void main(String[] args) {
        /*
        try {
            int res = test();
            System.out.println("one和two的商是:" + res);
        }
        catch (ArithmeticException e){
            System.out.println("除数不允许为零");
            e.printStackTrace();
        }
        catch (InputMismatchException e){
            System.out.println("不支持非数字");
            e.printStackTrace();
        }
        */

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

        }
        catch (InputMismatchException e){

        }
        catch (Exception e){

        }
//        test();  //只抛出父类Exception时这里报错
    }

    /*
    通过throws抛出异常时,针对可能出现的多种情况,解决方案:
    1. throws后面接多个异常类型,中间用逗号分隔
     */

    /*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 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;
    }
}

throw:抛出异常对象,抛出的只能是可抛出类Throwable或者其子类的实例对象

技术分享图片

 

 有两种处理方法

技术分享图片

一种是抛出异常类型对象,自己的方法进行处理异常

技术分享图片

 

一种是抛出异常,调用该方法处进行异常处理

技术分享图片

 

 

 

抛出异常-throws和throw

原文:https://www.cnblogs.com/mpp0905/p/10367508.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!