没有参数的异常问题

时间:2011-04-08 17:32:42

标签: c# .net exception

遇到这个事件,事件包装器和处理程序的一个奇怪的问题:

public delegate void StatusUpdateHandler(string message, Exception exc, SeverityLevel severity);

public event StatusUpdateHandler StatusUpdate;

private void FireStatusUpdate(string message)
{
    if (this.StatusUpdate != null)
        this.StatusUpdate(message, null, SeverityLevel.None);
}

void scanDocProcessor_StatusUpdate(string message, Exception exc, SeverityLevel severity)
{
    try
    {
        if (exc != null)
        {
            if (severity >= setSevLevel)
                this._logger.Log(message + Environment.NewLine + exc.ToString(), LogEntryType.Emergency, "OCR Submission Processor Status Update", true);
            else
                this._logger.Log(message + Environment.NewLine + exc.ToString(), LogEntryType.Error, "OCR Submission Processor Status Update", false);
        }
        else if (severity >= setSevLevel)
        {
            this._logger.Log(message, LogEntryType.Info, "OCR Submission Processor Status Update", true, true);
        }
        else
            this._logger.Log(message, LogEntryType.Info, "OCR Submission Processor Status Update", false);
    }
    catch (Exception)
    {
        EventLog.WriteEntry("Russia OCR Submission Processor", "Could not log status update event: " + exc.ToString(), EventLogEntryType.Information);
    }
}

在几分钟的时间内,_logger停止记录消息,而是在事件日志中收到这些消息:

  

无法记录状态更新事件:System.NullReferenceException:未将对象引用设置为对象的实例。      在ScannedService.scanDocProcessor_StatusUpdate(String message,Exception exc,SeverityLevel severity)      在ScannedService.Processor.FireStatusUpdate(String message)      在ScannedService.Processor.ProcessQueue(Object Object)

我很困惑,当应该编写exc.ToString()时,事件日志如何获得这样的堆栈跟踪。我查看了IL的scanDocProcessor_StatusUpdate方法没有初始化Exception对象。除此之外,我不知道如何抛出nullreferenceexception。当Log方法捕获异常时,它会吞下它或者用“throw;”重新抛出它。 message参数永远不为null,SeverityLevel是枚举。

1 个答案:

答案 0 :(得分:0)

您在其中else为空的ecx条件之一中抛出异常。在catch块中,您假设ecx不为null,这会生成隐藏原始的另一个异常

您可以使用以下命令使日志语句为空安全:

EventLog.WriteEntry("Russia OCR Submission Processor", 
    String.Format("Could not log status update event: {0}", exc), EventLogEntryType.Information);