C# 如何接受在catch块中抛出的异常?
下面是我的一些错误日志代码。当我的应用程序内部发生异常时,我会将其记录到数据库中。如果该数据库已关闭或出现其他问题,我会尝试将其记录在事件查看器中 如果事件查看器写入也因某种原因失败,会发生什么情况?我如何放弃或接受这个新的例外C# 如何接受在catch块中抛出的异常?,c#,.net,exception-handling,C#,.net,Exception Handling,下面是我的一些错误日志代码。当我的应用程序内部发生异常时,我会将其记录到数据库中。如果该数据库已关闭或出现其他问题,我会尝试将其记录在事件查看器中 如果事件查看器写入也因某种原因失败,会发生什么情况?我如何放弃或接受这个新的例外 void SaveLog(string accountId, Exception ex, Category category, Priority priority) { try { using (var connection = new
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 {
}
}