VB.NET 2010,Framework 3.5
不仅要捕获异常,还要确切地确定它是什么类型的错误。要在事件日志已满时创建错误,请写入PC的事件日志。下面的示例抛出System.ComponentModel.Win32Exception。这很好,除了它并没有告诉我“事件日志已满”,它可能是System.ComponentModel.Win32Exception所包含的其他错误。查看ErrorCode = -2147467259或NativeErrorCode = 1502以确定事件日志已满是安全吗?
Sub Main()
Try
Do While True ' this loop will overflow the Event Log
EventLog.WriteEntry("source", "message")
Loop
Catch w As System.ComponentModel.Win32Exception
If w.ErrorCode = -2147467259 Then
' will ErrorCode always = -2147467259 when The Event Log file is full?
End If
If w.NativeErrorCode = 1502 Then
' will NativeErrorCode always = 1502 when The Event Log file is full?
End If
If w.Message = "The event log file is full" Then
' code for event log is full
End If
If w.ErrorCode = -2147467259 Then
If w.NativeErrorCode = 1502 Then
If w.Message = "The event log file is full" Then
' code for event log is full if all 3 conditions are True?
End If
End If
End If
End Try
End Sub