chap7-3.txt

来自「清华大学出版社经典教材系列」· 文本 代码 · 共 31 行

TXT
31
字号
// 程序7-3
public class testFinally{
    public static void main(String args[ ]) {
        int a,b=0;
        
        for(int i=0;i<3;i++){
            System.out.println("Test No: "+(i+1));
            try{
                switch(i){
                    case 0:  a=3/b;			// 0作除数
                            break;
                    case 1:  int  c[ ]=new int[10];
                            c[10]=0;			// 数组下标越界
                            break;
                    case 2:  char ch="ABC".charAt(99);	//字符串的下标越界
                            break;
                    case 3:  return;			// 通过return语句结束程序
                }
            }catch(ArithmeticException  e) {
                System.out.println("零作除数!");
            }catch(ArrayIndexOutOfBoundsException  e) {
                System.out.println("数组下标越界!");
            }catch(IndexOutOfBoundsException  e) {
                System.out.println("下标越界!");
            }finally{
                System.out.println("在finally块中!");
            }
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?