try catch finally return?

来源:互联网 发布:网络设备网管软件 编辑:程序博客网 时间:2024/05/22 15:00
/** * try catch finally 中含有return 总结 * Created by nier_ni on 2014/8/6. */public class TrySequency {    /**     * try catch finally 没有异常 没有return 正常执行     */    public static void  tryNoException(){       try{           System.out.println("try块执行");       }catch(Exception e){            System.out.println("catch块执行");       }finally{           System.out.println("finally块执行");       }    }    /**     * try 中异常 catch finally     */    public static void tryException(){        int i = 0;        try {            System.out.println("try块执行");            int j = 5/i;        }catch (Exception e){            System.out.println("catch块执行");        }finally {            System.out.println("finally块执行");        }    }    /**     * try 中有return catch finally 没有异常     */   public static String tryReturnNoException(){        try {            System.out.println("tryReturnNoException中try");            return "try 中 return";        }catch (Exception e){            System.out.println("tryReturnNoException中catch");        }finally {            System.out.println("tryReturnNoException中finally");        }       return "0";   }    /**     * try 中有return catch finally 有异常     */    public  static String tryReturnException(){        int i = 0;        try {            System.out.println("tryReturnException中try");            int j = 5 / i;            return "try 中 return";        }catch (Exception e){            System.out.println("tryReturnException中catch");        }finally {            System.out.println("tryReturnException中finally");        }        return "0";    }    /**     * try catch finally 之后有return 没有异常     */    public static String tryNoExceptionFinallyReturn(){        try{            System.out.println("try块执行");        }catch(Exception e){            System.out.println("catch块执行");        }finally{            System.out.println("finally块执行");        }        return "finally之后的return";    }    /**     * try catch finally 之后有return 有异常     */    public static String tryExceptionFinallyReturn(){        int i = 0 ;        try{            System.out.println("try块执行");            int j = 3/0;        }catch(Exception e){            System.out.println("catch块执行");        }finally{            System.out.println("finally块执行");        }        return "finally之后的return";    }    /**     * try catch finally (catch finally中有return)返回那个return     */    public static int tryExceptionCatchFinallyReturnNoException(){        int i = 1;        try {            System.out.println("try块开始执行\t" + i );            int j = 5/i;        }catch(Exception e){            System.out.println("catch块开始执行\t" + i);            i++;            return i;        }finally {            System.out.println("finally块开始执行\t" + i);            i = 100;        }        return i;    }    /**     *try catch finally(catch finally 中有return)有异常 返回那个return     */    public static int tryExceptionCatchFinallyException(){        int i = 0;        try {            System.out.println("try块开始执行\t" + i );            int j = 5/i;        }catch(Exception e){            System.out.println("catch块开始执行\t" + i);            i++;            return i;        }finally {            System.out.println("finally块开始执行\t" + i);            i = 100;        }        return i;    }    public static void main(String[] args) {        /*System.out.println("try catch finally 没有异常 没有return 正常执行");        tryNoException();        System.out.println("----------try 中异常 catch finally------------");        tryException();        System.out.println("-----try 中有return catch finally 没有异常-----");        System.out.println(tryReturnNoException());        System.out.println("-----try 中有return catch finally 有异常-----");        System.out.println(tryReturnException());        System.out.println("-----try catch finally 之后有return 没有异常-----");        System.out.println(tryNoExceptionFinallyReturn());        */       /* System.out.println("-----try catch finally 之后有return 有异常-----");        System.out.println(tryExceptionFinallyReturn());        System.out.println("-------try catch finally (catch finally中有return)--------没有异常");        System.out.println(tryExceptionCatchFinallyReturnNoException());*/        System.out.println("------try catch finally(catch finally中有return)------有异常");        System.out.println(tryExceptionCatchFinallyException());    }}

0 0
原创粉丝点击