Java中的try、catch、finally塊簡單的解析

qingyezhu發表於2014-07-20
package com.wangzhu;

import java.util.HashMap;
import java.util.Map;

/**
 * 在try、catch、finally塊中,若try中有return語句,則返回try中變數的值,<br/>
 * 不管try塊外是否對該變數進行了修改, 都不影響try中return的返回值。<br/>
 * 若finally中有return語句,則忽略try、catch塊中的return語句。<br/>
 * 若finally中丟擲異常,則整個try-catch-finally塊中丟擲異常。<br/>
 * 
 * 注意:<br/>
 * 儘量在try或者catch中使用return語句,通過finally快中達到對try或者catch返回值的修改是不可行的。<br/>
 * finally塊中避免使用return語句,因為finally快中如果使用return語句,<br/>
 * 會顯示的消化掉try、catch塊中的異常資訊, 遮蔽了錯誤的發生。<br/>
 * finally塊中皮面再次丟擲異常,否則整個try-catch-finally塊的方法丟擲異常,並會消化掉try、catch塊中的異常。<br/>
 * 
 * @author wangzhu2014-7-20下午8:48:43
 * 
 */
public class CatchDemo1 {
    /**
     * 返回:try
     * 
     * @return
     */
    public static String test() {
        String str = "";
        try {
            str = "try";
            return str;
        } catch (Exception e) {
            str = "catch";
            return str;
        } finally {
            str = "finally";
        }
    }

    /**
     * 返回:finally
     * 
     * @return
     */
    public static String test1() {
        String str = "";
        try {
            str = "try";
            return str;
        } catch (Exception e) {
            str = "catch";
            return str;
        } finally {
            str = "finally";
            return str;
        }
    }

    /**
     * 返回:catch
     * 
     * @return
     */
    public static String test2() {
        String str = "";
        try {
            str = "try";
            Integer.parseInt(null);
            return str;
        } catch (Exception e) {
            str = "catch";
            return str;
        } finally {
            str = "finally";
        }
    }

    /**
     * 返回:finally
     * 
     * @return
     */
    public static String test3() {
        String str = "";
        try {
            str = "try";
            Integer.parseInt(null);
        } catch (Exception e) {
            str = "catch";
            return str;
        } finally {
            str = "finally";
            return str;
        }
    }

    /**
     * 返回:finally
     * 
     * @return
     */
    public static String test4() {
        String str = "";
        try {
            str = "try";
            Integer.parseInt(null);
            return str;
        } catch (Exception e) {
            str = "catch";
            Integer.parseInt(null);
            return str;
        } finally {
            str = "finally";
            // 若去掉return語句則最後會報異常
            return str;
        }
    }

    /**
     * 返回:finally
     * 
     * @return
     */
    public static String test5() {
        String str = "";
        try {
            str = "try";
            Integer.parseInt(null);
            return str;
        } catch (NullPointerException e) {
            str = "catch";
            return str;
        } finally {
            str = "finally";
            // 若去掉return語句則最後會報異常
            return str;
        }
    }

    public static String test6(Map<String, Object> map) {
        String str = "";
        try {
            str = "try";
            map.put(str, str);
            Integer.parseInt(null);
            return str;
        } catch (NullPointerException e) {
            str = "catch";
            map.put(str, str);
            return str;
        } finally {
            str = "finally";
            map.put(str, str);
            // 若去掉return語句則最後會報異常
            return str;
        }
    }

    public static void main(String[] args) {
        System.out.println("try: " + CatchDemo1.test());
        System.out.println("finally: " + CatchDemo1.test1());
        System.out.println("catch: " + CatchDemo1.test2());
        System.out.println("finally: " + CatchDemo1.test3());
        System.out.println("finally: " + CatchDemo1.test4());
        System.out.println("finally: " + CatchDemo1.test5());
        Map<String, Object> map = new HashMap<String, Object>();
        System.out.println("finally: " + CatchDemo1.test6(map));
        for (String key : map.keySet()) {
            System.out.println(key + "===" + map.get(key));
        }
    }
}

相關文章