try-catch-finally(C# 참조)
일반적으로 catch와 finally를 함께 사용하여 try 블록에서 리소스를 가져와 사용하고 catch 블록에서 예외 상황을 처리한 다음, finally 블록에서 리소스를 해제합니다.
예외를 다시 throw하는 것에 대한 자세한 내용 및 예제는 try-catch 및 예외 throw를 참조하십시오.
// try_catch_finally.cs
using System;
public class EHClass
{
static void Main()
{
try
{
Console.WriteLine("Executing the try statement.");
throw new NullReferenceException();
}
catch (NullReferenceException e)
{
Console.WriteLine("{0} Caught exception #1.", e);
}
catch
{
Console.WriteLine("Caught exception #2.");
}
finally
{
Console.WriteLine("Executing finally block.");
}
}
}
샘플 출력
Executing the try statement. System.NullReferenceException: Object reference not set to an instance of an object. at EHClass.Main() Caught exception #1. Executing finally block.