捕获EndOfStreamException异常
我想捕获EndOfStreamException异常,为何下面这段代码是死循环,不能捕获EndOfStreamException异常?应该是读到文件尾就触发EndOfStreamException异常的?
Function GetStringsFromFile(ByVal FileName As String) As Collection
Dim Strings As New Collection
Dim Stream As System.IO.StreamReader = System.IO.File.OpenText(FileName) 'Open the file.
Try
While True ' Loop terminates with an EndOfStreamException
' error when end of stream is reached.
Strings.Add(Stream.ReadLine())
End While
Catch eos As System.IO.EndOfStreamException
' No action is necessary; end of stream has been reached.
Catch IOExcep As System.IO.IOException
' Some kind of error occurred. Report error and clear collection.
MsgBox(IOExcep.Message)
Strings = Nothing
Finally
Stream.Close() ' Close the file.
End Try
Return Strings
End Function