Catch problem

D

Doug Gifford

WROX "Beginning VB.Net" 2nd Edition

Using this code from Chapter 11 Page 472:
Do
Try
' Read a line from the file...
currentLine = myReader.ReadLine
lineCounter = lineCounter + 1
Catch e As EndOfStreamException
' Display the message...
MessageBox.Show(e.Message)
' Exit the Do loop...
Exit Do
Finally
' Concatenate the data to the strData
variable...
currentData = currentData & currentLine & vbCrLf
End Try
Loop While currentLine <> Nothing

The text file I am reading has blank lines in it.
Sample:

Line one <CrLf>
Line two <CrLf>
<CrLf>
Line Four <CrLf>
Line Five <CrLf>
Line Six <CrLf>

When the code reaches Line Four it exit the loop because
currentLine = Nothing.

I want to read all the lines (one at a time) and exit the loop
when I have reached the end of the file. I still want to read past the blank
lines.

If I use a straight loop without the "While currentLine <>
Nothing" the "Catch e As EndOfStreamException" does not catch the end of
stream exception and enters an infinite loop!
How can I read the stream, one line at at time, and process
"ALL" the lines in the file?
 
J

John Doe

Hi Doug,

I'm just wondering whether you could do this and encapsulate the whole thing
in an appropriate try/catch block. EOF is "end of file" -- perhaps this is
what you really want to be testing for...

while not EOF(1)
your code...
loop


I'm a newbie at VB.NET, but it might be worth a try.
Sincerely,
Davis Bennett

P.S. If you try this, let me know how you make out
 
P

Phill. W

.. . .

OK, I'm new here too (using VB.Net 2003), but here goes
nothing...


Dim MyReader As New System.IO.StreamReader("d1.dat")
Dim sLine as String

sLine = MyReader.ReadLine()
Do While Not ( sLine Is Nothing )
' Do something

' Next Line
sLine = MyReader.ReadLine()
Loop

Seems to handle blank lines.

HTH,
Phill W.
 
J

Jay B. Harlow [MVP - Outlook]

Doug,
I normally follow a more 'positive' version of Phill's example:

Dim MyReader As New System.IO.StreamReader("d1.dat")
Dim sLine as String

Dim sb As New System.Text.StringBuilder

sLine = MyReader.ReadLine()

Do Until sLine Is Nothing
' Do something
lineCounter = lineCounter + 1
sb.Append(currentLine)
sb.Append(vbCrLf)

' Next Line
sLine = MyReader.ReadLine()
Loop
currentData = sb.ToString()

Which as Phill stated, handles blank lines.

StreamReader.ReadLine will return Nothing when the end of stream is found,
there is no "real" need to catch the EndOfStreamException to detect EOF.

Also notice that I am using a System.Text.StringBuilder to create the
currentData string, as concatenating strings in a loop is normally a bad
idea.

Hope this helps
Jay
 
D

Doug Gifford

Thanks for the help!

By changing the "Loop While statement" I was able to get the desired
results.
Changing to:
Loop While NOT currentLine IS Nothing
did the job!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top