JAVA--编译期的异常

package test;

/*try...finally
 * 1、发生了异常会向上抛出,但是finally里面的代码永远会得到执行
 * 2、无异常时,return后面可以通过finally来执行代码
 * try...catch..finally
 * 1、finally 里面适合做方法的资源关闭和收尾的工作
 */
public class Demo2 {
    public static void main(String[] args) {
        //divide(10,2);
        divide(10,0);
    }
    public static int divide(int a, int b) {
        int c = 0;
        try {
            c = a / b;
        } catch (Exception e) {
                c = 4;
                //方法遇到return就形成返回值,后续再修改变量就不会改变返回值,finally中的方法不执行
                return c;
        }finally {
            c = 5;
            System.out.println("1111111");
        }
        return c;
    }
}
 

你可能感兴趣的:(JAVA--编译期的异常)