Java学习七,异常处理

1.异常的处理

     我们常用的异常处理有以下关键字:try,catch,finally,throw,throws

                    

2.try-catch-finally

    基本的结构如图:

                               

   try:用于捕获异常;

   catch:用于处理try捕获到的异常;

   finally:无论是否发生异常代码,总是执行;

例子

package com_imooc.yichang;

import java.util.Scanner;

public class try_test {
    public static void main(String[] args) {
        System.out.print("程序开始,请输入第一个数字:");
        Scanner s = new Scanner(System.in);
        int one = s.nextInt();

        System.out.print("请输入第二个数字:");
        int two =s.nextInt();

        System.out.println("=====开始计算=====");
        System.out.println("one/two的结果是:"+one/two);
        System.out.println("运算结束");
    }
}

在上个代码中,如果不对键盘操作的数组或者字符做规范的处理,程序是很容易报错的,所以我们使用try-catch-finally来控制我们的代码;

package com_imooc.yichang;

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

public class try_test {
    public static void main(String[] args) {
        try {
            System.out.print("程序开始,请输入第一个数字:");
            Scanner s = new Scanner(System.in);
            int one = s.nextInt();

            System.out.print("请输入第二个数字:");
            int two = s.nextInt();

            System.out.println("=====开始计算=====");
            System.out.println("one/two的结果是:" + one / two);
        } catch (InputMismatchException e) {
            System.out.println("输入格式异常");
            e.printStackTrace();
        } catch (ArithmeticException e) {
            System.exit(1); //终止程序的运行
            System.out.println("算数异常");
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("你看,出错了吧");
            e.printStackTrace();
        } finally {
            System.out.println("运算结束");
        }
    }
}

值得注意的一点是:如果把try-catch-finally作为方法中运行时,finally代码块中是不允许return出现的.

3.throws

                      

                    

package com_imooc.yichang;

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

public class throws_test {
    /**
     *   throws的使用
     *   解决方法一,在方法后面声明具体的异常;
     * @return
     * @throws ArithmeticException
     * @throws InputMismatchException
     */
    public static int aaa() throws ArithmeticException,InputMismatchException {
        System.out.print("程序开始,请输入第一个数字:");
        Scanner s = new Scanner(System.in);
        int one = s.nextInt();

        System.out.print("请输入第二个数字:");
        int two = s.nextInt();

        System.out.println("=====开始计算=====");
        System.out.println("one/two的结果是:");
        return  one / two;
    }

    /**
     * 解决方法二,在方法后面声明异常Exception();
     * @return
     * @throws Exception
     */
    public static int bbb() throws Exception {
        System.out.print("程序开始,请输入第一个数字:");
        Scanner s = new Scanner(System.in);
        int one = s.nextInt();

        System.out.print("请输入第二个数字:");
        int two = s.nextInt();

        System.out.println("=====开始计算=====");
        System.out.println("one/two的结果是:");
        return  one / two;
    }


    public static void main(String[] args) {

        try {
            int rel =aaa();
        } catch (ArithmeticException e) {
            System.out.println("算法异常");
            e.printStackTrace();
        }catch (InputMismatchException e) {
            System.out.println("键盘输入异常");
            e.printStackTrace();
        }

        try {
            int rel =bbb();
        } catch (ArithmeticException e) {
            System.out.println("算法异常");
            e.printStackTrace();
        }catch (InputMismatchException e) {
            System.out.println("键盘输入异常");
            e.printStackTrace();
        }catch (Exception e) {
            System.out.println("异常");
            e.printStackTrace();
        }


    }
}

4.throw

                         

throw的异常处理有两种:

             

package com_imooc.yichang;

import java.util.Scanner;

public class throw_test {
    /**
     * throw的处理一: try-catch 自己处理自己
     */
    public static void age_one() {
        System.out.println("请输入年龄:");
        Scanner s = new Scanner(System.in);
        int one = s.nextInt();
        if (one < 18 || one > 80) {
            try {
                throw new Exception("18岁一下,80岁以上不得入内");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("欢迎光临入住");
        }
    }

    /**
     * throw的处理二: 先声明异常,调用的时候直接使用 try-catch 调用异常
     * @throws Exception
     */
    public static void age_two() throws Exception {
        System.out.println("请输入年龄:");
        Scanner s = new Scanner(System.in);
        int one = s.nextInt();
        if (one < 18 || one > 80) {
                throw new Exception("18岁一下,80岁以上不得入内");
        } else {
            System.out.println("欢迎光临入住");
        }
    }

    public static void main(String[] args) {
        age_one();

        try {
            age_two();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

5.常见的异常类

                                

                                

猜你喜欢

转载自blog.csdn.net/feiwutudou/article/details/86004426
今日推荐