다음을 통해 공유


방법: 예외 catch(Visual C#)

업데이트: 2007년 11월

이 예제에서는 try-catch 블록을 사용하여 0으로 나누기를 예외로 catch합니다. 예외를 catch한 후에 finally 블록에서 실행이 다시 시작됩니다.

예제

int top = 0, bottom = 0, result = 0;

try
{
    result = top / bottom;
}
catch (System.Exception ex)
{
    System.Console.WriteLine("{0} exception caught here.", ex.GetType().ToString());
    System.Console.WriteLine(ex.Message);
}
finally
{
    System.Console.WriteLine("Clean-up code executes here...");
}
System.Console.WriteLine("Program execution continues here...");
System.DivideByZeroException exception caught here.
Attempted to divide by zero.
Clean-up code executes here...
Program execution continues here...

코드 컴파일

코드를 복사한 다음 콘솔 응용 프로그램의 Main 메서드에 붙여넣습니다.

참고 항목

기타 리소스

Visual C# Express