异常处理之多重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");
    }
}

 

posted @ 2017-04-18 21:13  清风追梦enjoy  阅读(779)  评论(0编辑  收藏  举报