Microsoft MVP성태의 닷넷 이야기
try/finally 에 관해서 문의 드립니다. [링크 복사], [링크+제목 복사],
조회: 15914
글쓴 사람
최종문 (jm49.choi at samsung.com)
홈페이지
첨부 파일
 

안녕하세요.

try/finally에 관해 MSDN이나 CLR via C# 등의 책에서는
try{} 코드 블럭에서 예외가 발생하던 하지 않던 finally{} 블럭이 실행된다고 나와 있습니다.

예를 들면..
ex) http://msdn.microsoft.com/en-us/library/zwc8s4fz(VS.80).aspx
// try-finally
using System;
public class MainClass
{
    static void Main()
    {
        int i = 123;
        string s = "Some string";
        object o = s;

        try
        {
            // Invalid conversion; o contains a string not an int
            i = (int)o;
        }
        finally
        {
            Console.Write("i = {0}", i);
        }
    }
}
Although an exception was caught, the output statement included in the finally block will still be executed, that is:
i = 123
For more information on finally, see try-catch-finally.

위와 같이 try{} 블럭에서 예외가 발생했지만 finally{} 블럭이 실행된 것을 알 수 있습니다.

하지만 위의 코드를 VS2008에서 큰솔프로젝트에 위의 코드를 작성하고
Release/Debug로 빌드하여 실행해보면 finally{} 블럭이 실행되지 않습니다.

그런데 try/finally 사이에 catch(Exception e){} 블럭을 삽입하고 다시 실행하면
finally{} 블럭이 실행됩니다.


try/finally 만 사용했을 때, try 블럭에서 예외가 발생하면 MSDN에서는 finally가 무조건 실행된다고 하는데 막상 코드로 실행해보면 finally가 실행되지 않습니다. 왜 이런 것일까요?

감사합니다.








[최초 등록일: ]
[최종 수정일: 7/14/2010]


비밀번호

댓글 작성자
 




NoWriterDateCnt.TitleFile(s)