Java异常

2021-04-20  本文已影响0人  媛猿YY

异常处理

image.png
package com.example.javabase.gongju.exception;

import java.util.Scanner;

public class TryDemoOne {
    public static void main(String[] args) {


//要求:定义两个整数,接收用户的键盘输入,输出两数之商
        Scanner input = new Scanner(System.in);
        System.out.println("====运算开始====");
        try {
            System.out.print("前输入第一个整数");
            int one = input.nextInt();
            System.out.print("前输入第一个整数");
            int two = input.nextInt();
//        被除数为0、非数字时会异常
            System.out.println("one和two的⤴商是:" + (one / two));
        } catch (Exception e) {
            System.out.println("程序出错");
            e.printStackTrace();
        } finally {
            System.out.println("====运算结束====");
        }


    }

}

多重catch

package com.example.javabase.gongju.exception;

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

public class TryDemoOne {
    public static void main(String[] args) {


//要求:定义两个整数,接收用户的键盘输入,输出两数之商
        /*
         * ArithmeticException 算数异常
         * InputMismatchException 格式异常
         * */
        Scanner input = new Scanner(System.in);
        System.out.println("====运算开始====");
        try {
            System.out.print("前输入第一个整数");
            int one = input.nextInt();
            System.out.print("前输入第一个整数");
            int two = input.nextInt();
//        被除数为0、非数字时会异常
            System.out.println("one和two的商是:" + (one / two));
        } catch (ArithmeticException e) {
            System.out.println("程序不允许为0");
            e.printStackTrace();
        } catch (InputMismatchException e) {
            System.out.println("请输入整数");
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("出错了");
            e.printStackTrace();
        } finally {
            System.out.println("====运算结束====");
        }


    }

}
 System.exit(1);//终止程序运行
 public static int test() {
        Scanner input = new Scanner(System.in);
        System.out.println("====运算开始====");
        try {
            System.out.print("前输入第一个整数");
            int one = input.nextInt();
            System.out.print("前输入第一个整数");
            int two = input.nextInt();
            return one / two;
        } catch (ArithmeticException e) {
            System.out.println("除数不允许为0");
            return 0;
        } finally {
            System.out.println("====运算结束====");
//            return -100000; 不支持
        }


    }

throws

package com.example.javabase.gongju.exception;

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

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("除数不允许为0");
//            e.printStackTrace();
//        }catch (InputMismatchException e){
//            System.out.println("类型错误,请输入整数");
//            e.printStackTrace();
//        }
        try {
            int result = test();
            System.out.println("one和two的商是:" + result);
        } catch (Exception e) {

        }


    }

    /*
     * 通过throws 抛出异常时,针对啃出现的多种异常,解决方案
     * 1、throws后面接多个异常类型,中间用逗号分隔
     * 2、throws后面接Exception
     * */
//    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

throw new IOException();
  1. 通过try..catch包含throw语句,自己抛出自己处理
public static void main(String[] args) {
            testAge();
 
    }
    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("必须有亲友陪同");
            } else {
                System.out.println("欢迎入住");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
  1. 通过throws在方法声明处抛出异常类型--谁用谁处理--调用者可以自己处理
public static void main(String[] args) {
        try {
            testAge();
        } 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("必须有亲友陪同");
        } else {
            System.out.println("欢迎入住");
        }
    }

自定义异常

就是定义一个类,去继承Throwable类或者它的子类
自定义类 HotelAgeExecption.java

package com.example.javabase.gongju.exception;

public class HotelAgeExecption extends Exception {
    public HotelAgeExecption(){
        super("必须有亲友陪同");
    }
}

调用

public static void main(String[] args) {
        try {
            testAge();
        } catch (HotelAgeExecption e) {
            System.out.println(e.getMessage());
            System.out.println("酒店前台工作人员不允许办理入住");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
public static void testAge() throws HotelAgeExecption {

        System.out.println("请输入年龄:");
        Scanner input = new Scanner(System.in);
        int age = input.nextInt();
        if (age < 18 || age > 80) {
//            throw new Exception("必须有亲友陪同");
            throw new HotelAgeExecption();
        } else {
            System.out.println("欢迎入住");
        }
    }

异常链

package com.example.javabase.gongju.exception;

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

    }
    public static void testOne() throws HotelAgeExecption{
        throw new HotelAgeExecption();
    }
    public static void testTwo() throws Exception {
        try {
            testOne();
        } catch (HotelAgeExecption e) {
            throw new Exception("我是新产生的异常1",e);
        }
    }
    public static void testThree() throws Exception {
        try {
            testTwo();
        }  catch (Exception e) {
            throw new Exception("我是新产生的异常2",e);
        }
    }
}

上一篇 下一篇

猜你喜欢

热点阅读