天天看點

try catch 子產品 catch 部分有return 語句處理

import java.io.IOException;

/**
 * 
 */

/**
 * @author taoge
 *
 */
public class TestException {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		foo(0);
		foo(1);
	}
	
	public static void foo(int i){
			String str="";
			try {
				if (i==1) {
					throw new IOException();
					
				}
				str+=1;
			} catch (IOException e) {
				// TODO Auto-generated catch block
//				e.printStackTrace();
				str+=2;
				return;
			}finally{
				str+=3;
				System.out.println("test");
			}
			str+=4;
			System.out.println(str);
	}

}

           
test
134
test
           

由此可見,當catch 子產品遇到return 的時候,在return 之前執行 finally子產品語句,然後再執行return