2010-11-24 4 views
1

'static int Main (string [] args)'으로 시작하고 'EventRecievedProcessor'클래스의 인스턴스를 만든 다음에 메서드를 호출하는 C# Console 응용 프로그램이 있습니다. 인스턴스가 :System.NullReferenceException 첫 번째 시도/catch 블록에 걸려 있지 않음

static int Main(string[] args) 
{ 
    try 
    { 
     EventRecievedProcessor proc = new EventRecievedProcessor 

     if (!proc.Processs()) 
     { 
      Console.Write(Type + " processing failed. Please check logs for more information."); 
      Log.Error("Failed to process s"); 
      return (int)RETURNCODES.INTERNALAPPERROR; 
     } 
    } 
    catch (Exception ex) 
    { 
     // This is where the System.NullReferenceException from GetLatestEventInfo is currently being caught 

     Console.WriteLine("Exception message: " + ex.Message); 
     Console.WriteLine("Exception stack trace: " + ex.StackTrace); 

     Log.Fatal("An exception has been thrown. Message: " + ex.Message, ex); 

     return (int)RETURNCODES.INTERNALAPPERROR; 
    } 
} 

'EventRecievedProcessor'의 인스턴스는 기록의 컬렉션을 잡고 그 위에 foreach는 않습니다. 때라도 메서드를 호출 할

public class EventRecievedProcessor 
{ 

    public bool Processs() 
    { 
     List<Event> events = DAL.GetEvents; 

     foreach (Event e in events) 
     { 
      try 
      { 
       EventInfo lastEvent = Eventhistory.GetLatestEventInfo(e); 
      } 
      catch (Exception ex) 
      { 
       // Log exception and continue processing in foreach loop 

       // This is where I want to catch the NullReferenceException from GetLatestEventInfo 

       Log.DebugFormat("Error with eventid " + e.EventID); 
       Log.Error(ex); 
      } 
     } 

     return true; 
    } 
} 

하는 System.NullReferenceException가 발생 : 그것은 컬렉션의 각 레코드에 대한 '이벤트'클래스의 정적 메소드 (GetLatestEventInfo) 호출을

public class EventHistory 
{ 

    public static EventInfo GetLatestEventInfo(int customerCode, string premiscode) 
    { 

     EventInfo info = new EventInfo(); 

     // Do some work here... 
     // This is where the NullReferenceException is being generated. 

     return info; 

    } 
} 

때 NullReferenceException이 여기에 던져지면 foreach 루프의 catch 블록이 catch하고 로그에 기록한 다음 제어를 foreach 루프로 반환하여 처리를 계속할 것으로 기대합니다. 대신 예외가 최상위 'Main'메소드에서 발견되어 앱이 중단되고 나머지 레코드는 처리되지 않습니다.

예외가 첫 번째 catch 블록을 우회하는 방법/이유는 무엇입니까? 내가 여기서 잘못하고있는 것에 대한 아이디어가 있습니까?

스택 추적을 추가 :

System.NullReferenceException : 개체 참조가 개체의 인스턴스로 설정되지 않았습니다. C : \ Dev \ release6.01.100 \ Events \ EventProcessor \ EventProcessor \ EventHistory.cs의 EventProcessor.EventHistory.GetLatestEventInfo (이벤트 e)에서 : 65 행의 EventProcessor.Processors.EventProcessor.Process() C : \ Dev \ release6.01.100 \ Events \ EventProcessor \ EventProcessor \ Program.cs의 EventProcessor.Program.Main (String [] args)에있는 행 32 \ events \ EventProcessor \ EventProcessor \ Processors \ EventProcessor.cs : line 132

이름이 불투명하다면 죄송합니다. 이것은 작업 코드이므로 프라이버시 충돌을 피하기 위해 조금 변경하려고 시도했습니다.

+0

당신이 우리에게 예외가 표시 될 때의 스택 추적을 보여줄 수 있을까? –

+0

'Event.GetLatestEventInfo' 정적 메소드를 호출하면 (다른 클래스의)'EventHistory.GetLatestEventInfo'의 소스를 보여줍니다. 이 오자입니까, 아니면 그렇게되어야합니까? – NOtherDev

+0

GetLatestEventInfo에 대한 호출이 스택 추적을보고 예외를 발생시키는 지 확인 했습니까? 어쩌면 DAL.GetEvents도 GetlatestEventInfo를 호출합니까? –

답변

2

아무 것도 우회하지 않습니다. 스택 트레이스를 자세히 살펴보십시오. Console.WriteLine(ex.ToString());을 사용하십시오. 예외가 생각했던 곳에서 예외가 던져지지 않는 것을 볼 수 있습니다.

+0

Jon에게 의견을 보내 주셔서 감사합니다. 좀 더 확대 할 수 있니? 스택 추적은 EventHistory.GetLatestEventInfo()를 예외 소스로 식별합니다. – rgeorge

+0

예외 소스는'EventHistory.GetLatestEventInfo()'의 _inside_이며 try/catch 블록이 없습니다. –

+0

수정. 그래서 내 기대는 try/catch 블록에있는 호출 코드에 예외가 발생한다는 것입니다. – rgeorge

0

단순히 이것은 사실이 아니다 여기에 증거 :

class Program 
{ 
    static void Main() 
    { 
     // no need of try/catch here as exceptions won't propagate to here 
     Looper(); 
    } 

    static void Looper() 
    { 
     int processedRecords = 0; 
     for (int i = 0; i < 10; i++) 
     { 
      try 
      { 
       Thrower(i); 
       processedRecords++; 
      } 
      catch (NullReferenceException ex) 
      { } 
     } 
     // prints 5 as expected 
     Console.WriteLine("processed {0} records", processedRecords); 
    } 

    static void Thrower(int i) 
    { 
     if (i % 2 == 0) 
     { 
      throw new NullReferenceException(); 
     } 
    } 
}