달력

4

« 2024/4 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

'catch'에 해당되는 글 2

  1. 2013.04.11 자바 소스 디컴파일 해보기.
  2. 2007.04.23 try catch finally 사용시 주의해야 할 점! 1
2013. 4. 11. 15:05

자바 소스 디컴파일 해보기. 그거/Java2013. 4. 11. 15:05


서버에 있는 class 파일과 PC 에 있는 class 파일의 크기가 달라서

서버의 class 파일과 PC의 class 파일을 decompile 해서 비교하다 보니 아래와 같이 알 수 없는 문장(?)들이 있어서


        break MISSING_BLOCK_LABEL_361;

        Exception exception;

        exception;

        if(bw != null)

            bw.close();

        throw exception;

        if(bw != null)

            bw.close();

그래서, decompile 테스트 해봤다.


BufferedReader br = null;

try {

System.out.println("try >>");

br = new BufferedReader(new InputStreamReader(System.in));

br.readLine();

System.out.println("try <<");

} catch(Exception e) {

System.out.println("catch >>");

e.printStackTrace();

System.out.println("catch <<");

} finally {

System.out.println("finally >>");

if( br != null ) {

try { 

System.out.println("finally try >>");

br.close(); br = null; 

System.out.println("finally try <<");

} catch(Exception ignr) {

System.out.println("finally catch");

}

}

System.out.println("finally <<");

}

System.out.println("000");

ArrayList list = new ArrayList();

for(int i = 0; i < list.size();) {

Object o = list.get(0);

break;

}

System.out.println("111");


int j = 0;

if( j < list.size() ) {

Object o = list.get(0);

}

System.out.println("222");


for(int i = 0; i < list.size(); i++) {

Object o = list.get(i);

}

System.out.println("333");


위의 자바 소스를 컴파일 한 후 class 파일을 디컴파일 해보면 아래와 같다.


        BufferedReader bufferedreader = null;

        System.out.println("try >>");

        bufferedreader = new BufferedReader(new InputStreamReader(System.in));

        bufferedreader.readLine();

        System.out.println("try <<");

        System.out.println("finally >>");

        if(bufferedreader != null)

            try

            {

                System.out.println("finally try >>");

                bufferedreader.close();

                bufferedreader = null;

                System.out.println("finally try <<");

            }

            catch(Exception exception)

            {

                System.out.println("finally catch");

            }

        System.out.println("finally <<");

        break MISSING_BLOCK_LABEL_234;

        Exception exception1;

        exception1;

        System.out.println("catch >>");

        exception1.printStackTrace();

        System.out.println("catch <<");

        System.out.println("finally >>");

        if(bufferedreader != null)

            try

            {

                System.out.println("finally try >>");

                bufferedreader.close();

                bufferedreader = null;

                System.out.println("finally try <<");

            }

            catch(Exception exception2)

            {

                System.out.println("finally catch");

            }

        System.out.println("finally <<");

        break MISSING_BLOCK_LABEL_234;

        Exception exception3;

        exception3;

        System.out.println("finally >>");

        if(bufferedreader != null)

            try

            {

                System.out.println("finally try >>");

                bufferedreader.close();

                bufferedreader = null;

                System.out.println("finally try <<");

            }

            catch(Exception exception4)

            {

                System.out.println("finally catch");

            }

        System.out.println("finally <<");

        throw exception3;

        System.out.println("000");

        ArrayList arraylist = new ArrayList();

        int i = 0;

        Object obj;

        if(i < arraylist.size())

            obj = arraylist.get(0);

        System.out.println("111");

        i = 0;

        if(i < arraylist.size())

            obj = arraylist.get(0);

        System.out.println("222");

        for(int j = 0; j < arraylist.size(); j++)

        {

            Object obj1 = arraylist.get(j);

        }


        System.out.println("333");

        return;


디컴파일한 소스를 보면 finally 가 3번이나 나온다.

그리고, 1번만 실행되는 for문(2번 이상 실행되지 않는 for문)은

알아서 for 문의 조건만 포함한 if문으로 변경된다.


아. 젤 위에 있는 이상한 문장(?)들은 catch 와 finally 에 있는 코드들인듯 하돠.

뭐 새로울 건 없지만, 소스 분석하다가 재밌어서 올려봄.. ㅡ,.ㅡ;

'그거 > Java' 카테고리의 다른 글

JSP 의 EL 과 JSTL  (0) 2013.08.09
enum 사용하기  (0) 2013.05.02
Runtime Data Access  (0) 2012.07.30
NoClassDefFoundError vs ClassNotFoundException  (0) 2011.10.11
Eclipse 에서 Maven 이용해서 remote repository 에 deploy 하기  (0) 2011.10.05
:
Posted by 뽀기
2007. 4. 23. 10:43

try catch finally 사용시 주의해야 할 점! 그거/Java2007. 4. 23. 10:43

public class ThrowsTest {
 public static void main(String[] args){
  ThrowsTest tt = new ThrowsTest();
  tt.check();
  System.out.println("11");
 }

 String check() {
  try {
   Integer.parseInt("abc");
  } catch(Exception e) {
   throw e;
  } finally {
   return "";
  }
 }
}


위 소스를 보고 이상한점이 없는가?

check() 라는 method를 보면 catch block에서 exception을 throw 하는데

check method 선언부에서는 exception을 throws 하지 않고 있다.

즉, 컴파일 error가 발생해야 한다.

하지만!!!!!!

위 소스를 컴파일해보라... 놀랄것이다.

에러가 나지 않는다. 허허.. 이런 -_-;

check method를 잘 보면,

finally block에 return 문이 있다.

바로 이 finally block의 return 문이 catch block의 throw를 먹어버린것이다!

finally block의 return 문을 finally 밖으로 빼고 다시 컴파일 해보라..

컴파일 에러가 날것이다..

흔히들 try catch finally block을 함께 사용한다.

그리고, finally block은 무조건 실행되기 때문에 finally block에 return 문을 위치하는 경우가 종종있다.

이는 위험 천만한 발상인 것이다!

위 예에서처럼 try block에서 exception이 발생해서 catch block이 실행되어야 하는데

finally block에 return 문이 있으면 catch block의 exception을 먹어버려서

exception 발생을 감지하지 못하게 되는 것이다!!

무슨 일이 있어도..

finally block 안에 return 문은 넣지 말지어다!!!!!!!!

:
Posted by 뽀기