繁体   English   中英

如何吞下catch块中抛出的异常?

[英]How can I swallow an exception that is thrown inside a catch block?

下面是我的一些错误记录代码。 当我的应用程序内发生异常时,我将其记录到数据库中。 如果该数据库已关闭或存在其他问题,我会尝试在事件查看器中记录它。

如果事件查看器由于某种原因写入失败会发生什么? 我如何放弃或吞下这个新例外?

void SaveLog(string accountId, Exception ex, Category category, Priority priority)
{
    try
    {
        using (var connection = new SqlConnection(…))
        {
            connection.Open();
            command.ExecuteNonQuery();
        }
    }
    catch (Exception exception)
    {
        // exception while logging!   
        using (var eventLog = new EventLog { Source = "tis" })
        {
            eventLog.WriteEntry(
                exception.Message + Environment.NewLine + 
                exception.StackTrace,
                EventLogEntryType.Error);
        }
    }
}
try {
    // ...
}
catch (Exception exception) {
    try {
        // Attempt to write to event log.
    }
    catch {
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM