java异常的使用

package com.zzu.java.error;
/**
 * 异常的练习
 * @author 程路超
 *
 */
public class TestError {
    public static void main(String[] args) {
        
        System.out.println(tte());
        
        //多重catch语句
        try {
            int a = args.length;
            int b = 21 / a;           //除0异常
            int c[] = { 12 };
            c[23] = 99;               //数组下标越界异常;
        } catch (ArithmeticException e) {
            System.out.println(e);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println(e);
        } finally {
            System.out.println("警告程序异常!");
        }
    }
    
    //try,catch,finally 中return的作用;
    //执行优先级  return3,return2;没有异常执行return1
    public static int tte(){
        try {
            System.out.println("我是try");
            int a = 12 / 0;
            return 1;
        } catch (Exception e) {
            System.out.println("我是异常catch");
            return 2;
        } finally {
            System.out.println("我是finally");
            return 3;
        }
    }
    }

 

posted @ 2017-05-31 10:17  路迢迢  阅读(215)  评论(0编辑  收藏  举报