java的try-catch-finally批量异常处理例子

package com.heeexy.example.util;

/*
* 处理
* */
public class Test {

    public static void main(String[] args) throws Exception {

        String[] members = new String[4];
        for (int count = 0; count < 10; count++) {
            try {
                System.out.println("---------------------------");
                System.out.println(count);
                members[4] = new String(String.valueOf(count));
                System.out.println("---------------------------");
            }catch (ArrayIndexOutOfBoundsException e){
                System.out.println("出异常了,数组下标越界");
            }
            finally {
                System.out.println("finally必定执行");
            }
        }

    }
}

//运行结果
0
出异常了,数组下标越界
finally必定执行
---------------------------
1
出异常了,数组下标越界
finally必定执行
---------------------------
2
出异常了,数组下标越界
finally必定执行
---------------------------
3
出异常了,数组下标越界
finally必定执行
---------------------------
4
出异常了,数组下标越界
finally必定执行
---------------------------
5
出异常了,数组下标越界
finally必定执行
---------------------------
6
出异常了,数组下标越界
finally必定执行
---------------------------
7
出异常了,数组下标越界
finally必定执行
---------------------------
8
出异常了,数组下标越界
finally必定执行
---------------------------
9
出异常了,数组下标越界
finally必定执行

Process finished with exit code 0
 

猜你喜欢

转载自blog.csdn.net/qq_29726869/article/details/81284832