System.NullReferenceException未在第一次尝试/捕获块中被捕获

时间:2010-11-24 21:49:52

标签: c# .net exception-handling try-catch

我有一个以'static int Main(string [] args)'开头的C#Console应用程序,创建一个'EventRecievedProcessor'类的实例,然后在实例上调用一个方法:

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'的实例抓取一组记录并对其进行预测。它为集合中每条记录的'Event'类调用一个静态方法(GetLatestEventInfo):

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;
    }
}

调用follwoing方法时,抛出System.NullReferenceException:

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块捕获它,记录它,然后将控制返回到foreach循环以继续处理。相反,异常被捕获在顶级“主”方法中,这意味着应用程序将中止,其余记录不会被处理。

我不知道如何/为什么异常会绕过第一个catch块。关于我在这里做错了什么想法?

添加堆栈跟踪:

System.NullReferenceException:未将对象引用设置为对象的实例。    在C:\ Dev \ release6.01.100 \ Events \ EventProcessor \ EventProcessor \ EventHistory.cs中的EventProcessor.EventHistory.GetLatestEventInfo(事件e)中:第65行    在C:\ Dev \ release6.01.100 \ Events \ EventProcessor \ EventProcessor \ Processors \ EventProcessor.cs中的EventProcessor.Processors.EventProcessor.Process():第32行    在C:\ Dev \ release6.01.100 \ Events \ EventProcessor \ EventProcessor \ Program.cs中的EventProcessor.Program.Main(String [] args):第132行

很抱歉,如果我已经咀嚼了一些名字。这是工作代码,因此我尝试将其更改一点以避免任何隐私冲突。

2 个答案:

答案 0 :(得分:2)

它没有绕过任何东西。仔细查看堆栈跟踪。使用Console.WriteLine(ex.ToString());。你会看到异常并没有被你想象的抛出。

答案 1 :(得分: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();
        }
    }
}