Java

Try Catch Finally 동작순서

MuGrammer 2014. 12. 31. 13:04
public class closeTest {

	public static void main(String[] args) {
		
		String temp = tryTest();
		
		System.out.println(temp);
		

	}
	
	private static String tryTest(){
		
		String temp = null;
		
		try {
			System.out.println("1. Try Start - " + temp);
			if(temp != null) {
				temp += " Try";
			}
			else {
				throw new Exception("Force occure exception : ");
				
			}
			System.out.println("1. Try End - " + temp);
		}
		catch(Exception ex) {
			System.out.println("2. Exception Start - " + temp);
			temp += " Exception";
			System.out.println("2. Exception End - " + temp);
			
			return "Exception return : "+ temp;
			
		}
		finally{
			System.out.println("3. Finally Start - " + temp);
			temp += " > Finally";
			System.out.println("4. Finally End - " + temp);
		}
		
		
		return "### Success - "+temp;
	}
	
}


오류가 발생하지 않았을 경우

1. Try Start - TEST

1. Try End - TEST Try

3. Finally Start - TEST Try

4. Finally End - TEST Try > Finally

### Success - TEST Try > Finally


오류가 발생했을 경우

1. Try Start - null

2. Exception Start - null

2. Exception End - null Exception

3. Finally Start - null Exception

4. Finally End - null Exception > Finally

Exception return : null Exception




반응형

'Java' 카테고리의 다른 글

[JAVA/JSP] GET방식 한글깨짐 처리  (0) 2015.04.14
[Java] SimpleDateFormat  (0) 2015.04.02
파일읽기 기본  (0) 2013.12.31
Calendar 특정일자로 지정하기  (0) 2013.10.15
Java Compiler  (0) 2013.03.07