다음을 통해 공유


마지막으로

In addition to try and catch clauses, CLR exception handling supports a finally clause. The semantics are identical to the __finally block in structured exception handling (SEH). A __finally block can follow a try or catch block.

설명

The purpose of the finally block is to clean up any resources left after the exception occurred. Note that the finally block is always executed, even if no exception was thrown. The catch block is only executed if a managed exception is thrown within the associated try block.

finally는 대/소문자를 구분하는 키워드입니다. 자세한 내용은 상황에 맞는 키워드를 참조하십시오.

예제

The following example demonstrates a simple finally block:

// keyword__finally.cpp
// compile with: /clr
using namespace System;

ref class MyException: public System::Exception{};

void ThrowMyException() {
   throw gcnew MyException;
}

int main() {
   try {
      ThrowMyException();
   }
   catch ( MyException^ e ) {
      Console::WriteLine(  "in catch" );
      Console::WriteLine( e->GetType() );
   }
   finally {
      Console::WriteLine(  "in finally" );
   }
}
  

참고 항목

기타 리소스

예외를 /clr을 처리 합니다.