2012-12-28 3 views
1

두 가지 방법을 호출하는 멀티 캐스트 대리인이 있습니다. 첫 번째 메소드와 그 처리에 예외가있는 경우 두 번째 메소드 호출을 계속하는 방법은 무엇입니까? 나는 아래 코드를 첨부하고있다. 아래 코드에서 첫 번째 메서드는 예외를 throw합니다. 하지만 멀티 캐스트 대리인 호출을 통해 두 번째 방법을 계속 실행하는 방법을 알고 싶습니다. 루프 내부멀티 캐스트 대리자의 예외 처리

public delegate void TheMulticastDelegate(int x,int y); 
    class Program 
    { 
      private static void MultiCastDelMethod(int x, int y) 
      { 
        try 
        { 
          int zero = 0; 
          int z = (x/y)/zero; 
        } 
        catch (Exception ex) 
        {        
          throw ex; 
        } 
      } 

      private static void MultiCastDelMethod2(int x, int y) 
      { 
        try 
        { 
          int z = x/y; 
          Console.WriteLine(z); 
        } 
        catch (Exception ex) 
        { 
          throw ex; 
        } 
      } 
      public static void Main(string[] args) 
      { 
        TheMulticastDelegate multiCastDelegate = new TheMulticastDelegate(MultiCastDelMethod); 
        TheMulticastDelegate multiCastDelegate2 = new TheMulticastDelegate(MultiCastDelMethod2); 

        try 
        { 
          TheMulticastDelegate addition = multiCastDelegate + multiCastDelegate2; 

          foreach (TheMulticastDelegate multiCastDel in addition.GetInvocationList()) 
          { 
            multiCastDel(20, 30); 
          } 
        } 
        catch (Exception ex) 
        { 
          Console.WriteLine(ex.Message); 
        } 

        Console.ReadLine(); 
      } 
    } 
+0

왜 루프 안에 오류 처리를 넣지 않으시겠습니까? 예외가 발생하면 루프를 계속 진행하십시오. – mdcuesta

+0

의견에 감사드립니다. –

답변

1

이동시켜 try..catch : 전자가 필요로하는 새로운 예외를 생성로

foreach (TheMulticastDelegate multiCastDel in addition.GetInvocationList()) 
{ 
    try 
    { 
     multiCastDel(20, 30); 
    } 
    catch (Exception ex) 
    { 
     Console.WriteLine(ex.Message); 
    } 
} 

당신이 throw ;throw ex;을 대체 것 외에. 모양은 다음과 같습니다 :

private static void MultiCastDelMethod(int x, int y) 
{ 
    try 
    { 
     int zero = 0; 
     int z = (x/y)/zero; 
    } 
    catch (Exception ex) 
    { 
     throw ; 
    } 
} 
+0

try catch를 추가 할 필요가 없습니다. 당신이 아무것도 처리하지 않고 단지 예외를 던지면서. – shankbond