Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无参数捕获异常的问题_C#_.net_Exception - Fatal编程技术网

C# 无参数捕获异常的问题

C# 无参数捕获异常的问题,c#,.net,exception,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)

此事件、事件包装器和处理程序遇到奇怪问题:

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);
    }
}
几分钟后,日志记录器停止记录消息,而我在事件日志中收到了这些消息:

无法记录状态更新事件:System.NullReferenceException:对象引用未设置为对象的实例。 在ScannedService.scanDocProcessor_Status UpdateString消息中,异常exc,严重级别 在ScannedService.Processor.FireStatusUpdateString消息中 在ScannedService.Processor.ProcessQueueObject对象上


我对事件日志在编写exc.ToString时如何获得这样的堆栈跟踪感到困惑。我查看了scanDocProcessor\u StatusUpdate方法的IL,该方法未初始化异常对象。除此之外,我不知道如何抛出nullreferenceexception。当Log方法捕捉到异常时,它会将其吞并或使用throw;重新抛出;。消息参数从不为null,SeverityLevel是一个枚举。

在ecx为null的其他条件之一中,您正在引发异常。在catch块中,假设ecx不为null,这将生成另一个异常,隐藏原始异常

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

EventLog.WriteEntry("Russia OCR Submission Processor", 
    String.Format("Could not log status update event: {0}", exc), EventLogEntryType.Information);
除非您打算退出程序,否则不要捕获异常。