zoukankan      html  css  js  c++  java
  • 抛出异常-throws和throw

    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或者其子类的实例对象

     有两种处理方法

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

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

    
    
  • 相关阅读:
    C# EPPlus 导出Excel
    NetCore +EF+Mysql 从数据库生成实体类到项目
    VBA链接SQL server数据库
    sqlserver中的 binary varbinary image
    sql server DateTime与DateTime2的区别
    Sql Server增删改查字段的语法
    c#中queue的用法
    Sql Server中不相关的两个数据表的全部显示
    IActionResult的返回值类型
    linux内存映射
  • 原文地址:https://www.cnblogs.com/mpp0905/p/10367508.html
Copyright © 2011-2022 走看看