IT_Programming/C#

throw문

JJun ™ 2006. 2. 27. 15:55

Throw문은 프로그래머가 에러 조건에서 예외를 직접 던질 때사용한다.

Exception Message프로퍼티를 열어보면 예외에 대한 정보를 얻을 수 있는데,

throw문을 이용하면 직접 예외에 대한 정보를 입력하여 만들 수 있다.

 

throw(문장);

 

====================================================================

 

using System;

 

public class MainApp
{
 public static void Main()
 {
  try
  {
   rethrow();
  }
  catch (Exception e)
  {
   Console.WriteLine("여기는 Main()입니다.");
   Console.WriteLine(e.Message);
   Console.WriteLine("예외가 발생한 곳은 {0}입니다.",e.Source);
  }
 }
 public static void rethrow()
 {
  int a=0;

  try
  {
   if(a == 0)
   {
    throw(new Exception(one은 '0'이 아니라 '1'입니다."));
   }
  }
  catch (Exception e)
  {
   Console.WriteLine("여기는 rethrow()입니다.");
   Console.WriteLine(e.Message);
   Console.WriteLine("예외가 발생한 곳은 {0}입니다.",e.Source);
   throw; // 예외처리를 마친 후 다시 예외를 던집니다.
  }
 }
}

'IT_Programming > C#' 카테고리의 다른 글

이벤트와 델리게이트  (0) 2006.07.27
static  (0) 2006.04.07
System Exception  (0) 2006.02.27
예외처리  (0) 2006.02.27
프로퍼티  (0) 2006.02.27