java异常处理机制

http://blog.youkuaiyun.com/hguisu/article/details/6155636

异常和错误的区别:异常能被程序本身可以处理,错误是无法处理。


除了RuntimeException及其子类以外,其他的Exception类及其子类都属于可查异常。这种异常的特点是Java编译器会检查它,也就是说,当程序中可能出现这类异常,要么用try-catch语句捕获它,要么用throws子句声明抛出它,否则编译不会通过。


finally子句。它表示无论是否出现异常,都应当执行的内容。

当在try块或catch块中遇到return语句时,finally语句块将在方法返回之前被执行。在以下4种特殊情况下,finally块不会被执行:
1)在finally语句块中发生了异常。
2)在前面的代码中用了System.exit()退出程序。
3)程序所在的线程死亡。
4)关闭CPU。


finally与return

原则:

finally中不应该包含return语句的,finally仅做资源清理用;

在 finally 块中抛出的任何异常都会覆盖掉在其前面由 try 或者 catch 块抛出异常,包含 return 语句的情形相似。


1)在try语句中,在执行return语句时,要返回的结果已经准备好了,就在此时,程序转到finally执行了

在转去之前,try中先把要返回的结果存放到不同于a的局部变量中去,执行完finally之后,在从中取出返回结果, 因此,即使finally中对变量进行了改变,但是不会影响返回结果。

2)若finally中包含return,则最后返回以此为准

public static void main(String[] args) {  
    // TODO Auto-generated method stub  
    System.out.println(get());  
}  
  
public static boolean get() {  
  
    try {  
        System.out.println("try");  
        return true;  
  
    } catch (Exception e) {  
        e.printStackTrace();  
    } finally {  
        System.out.println("finally");  
    }  
    return false;  
} 

输出:

try

finally

true

class C {  
    int c = 100;  
  
    int get() {  
        return c;  
    }  
}  
public static void main(String[] args) {  
    // TODO Auto-generated method stub  
    System.out.println(get());  
}  
  
public static boolean get() {  
    C c = null;  
    try {  
        c = new C();  
        System.out.println("try");  
        return (c.get() == 100);  
    } catch (Exception e) {  
        e.printStackTrace();  
    } finally {  
        c = null;  
        System.out.println(c);  
        System.out.println("finally");  
    }  
    return false;  
}  

输出:

try
null
finally
true

 public static int test2() 
    { 
        int i = 1; 
        try 
        { 
            throw new Exception(); 
        } 
        catch 
        { 
            return ++i; 
        } 
        finally 
        { 
            ++i; 
            Console.WriteLine("finally:" + i); 
        } 
    } 
    static void Main(string[] args) 
    { 
        Console.WriteLine("Main:" + test2()); 
    } 

输出:

finally:3 
Main:2 

3)return语句的优先级是finally最大,且try、catch两者之一的return会被执行

public static int test2() 
    { 
        int i = 1; 
        try 
        { 
            throw new Exception(); 
        } 
        catch 
        { 
            return ++i; 
        } 
        finally 
        { 
            ++i; 
            Console.WriteLine("finally:" + i);
            return i; 
        } 
    } 
    static void Main(string[] args) 
    { 
        Console.WriteLine("Main:" + test2()); 
    } 
输出:

finally:3 
Main:3


一个很好的思考题:

package Test;  
  
public class TestException {  
    public TestException() {  
    }  
  
    boolean testEx() throws Exception {  
        boolean ret = true;  
        try {  
            ret = testEx1();  
        } catch (Exception e) {  
            System.out.println("testEx, catch exception");  
            ret = false;  
            throw e;  
        } finally {  
            System.out.println("testEx, finally; return value=" + ret);  
            return ret;  
        }  
    }  
  
    boolean testEx1() throws Exception {  
        boolean ret = true;  
        try {  
            ret = testEx2();  
            if (!ret) {  
                return false;  
            }  
            System.out.println("testEx1, at the end of try");  
            return ret;  
        } catch (Exception e) {  
            System.out.println("testEx1, catch exception");  
            ret = false;  
            throw e;  
        } finally {  
            System.out.println("testEx1, finally; return value=" + ret);  
            return ret;  
        }  
    }  
  
    boolean testEx2() throws Exception {  
        boolean ret = true;  
        try {  
            int b = 12;  
            int c;  
            for (int i = 2; i >= -2; i--) {  
                c = b / i;  
                System.out.println("i=" + i);  
            }  
            return true;  
        } catch (Exception e) {  
            System.out.println("testEx2, catch exception");  
            ret = false;  
            throw e;  
        } finally {  
            System.out.println("testEx2, finally; return value=" + ret);  
            return ret;  
        }  
    }  
  
    public static void main(String[] args) {  
        TestException testException1 = new TestException();  
        try {  
            testException1.testEx();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
}  

 通常认为输出为:

i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
testEx1, catch exception
testEx1, finally; return value=false
testEx, catch exception
testEx, finally; return value=false

红色部分其实不会输出。若去掉finally中的return语句,则会有上面的输出。原因见上面原则部分(finally做return时覆盖try或者catch中的异常)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值