zoukankan      html  css  js  c++  java
  • 异常处理之多重catch

    package com.sxt.exception.test1;
    
    import java.util.InputMismatchException;
    import java.util.Scanner;
    
    /*
     * 异常处理之多重catch
     * ArrayIndexOutOfBoundsException:数组越界异常
     * ArithmeticException:算术异常
     * InputMismatchException:输入类型不匹配异常
     * 
     * 多重catch时需要考虑异常的子父类关系 子类在上 父类在下
     * 执行顺序:try语句块->相应的catch语句块-->finally语句块-->执行到最后
     */
    public class Test2 {
        public static void main(String[] asrgs) {
            
            try {
                int []arr = new int[10];
                System.out.println(arr[10]);//越界
                
                int a = 10;
                int b = 0;
                int result = a/b;//除0
                
                Scanner input = new Scanner(System.in);
                System.out.println("请输入");
                int temp = input.nextInt();//输入String
                
            }catch(ArrayIndexOutOfBoundsException e) {
                e.printStackTrace();
            }catch (ArithmeticException e) {
                e.printStackTrace();
            }catch (InputMismatchException e) {
                e.printStackTrace();
            }catch (Exception e) {
                //e.printStackTrace();
                System.out.println("出现异常");
            }
            System.out.println("GameOver");
        }
    }
  • 相关阅读:
    BZOJ1452 count (树状数组)
    splay
    CODEVS1222 信与信封问题 (匈牙利算法)
    CODEVS1022 覆盖 (二分图染色+匈牙利算法)
    反思——针对作业的反思
    学习进度
    软件工程个人作业01
    阅读笔记
    安装SQL Server 2014
    软件工程概论_作业01
  • 原文地址:https://www.cnblogs.com/qingfengzhuimeng/p/6730469.html
Copyright © 2011-2022 走看看