PC Review


Reply
Thread Tools Rate Thread

Catch problem

 
 
Doug Gifford
Guest
Posts: n/a
 
      9th Dec 2003
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?






 
Reply With Quote
 
 
 
 
Tom Leylan
Guest
Posts: n/a
 
      9th Dec 2003
"Doug Gifford" <(E-Mail Removed)> wrote...

> Loop While currentLine <> Nothing


Try:

Loop Until currentLine is Nothing

Tom


 
Reply With Quote
 
John Doe
Guest
Posts: n/a
 
      9th Dec 2003
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


"Doug Gifford" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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?
>
>
>
>
>
>



 
Reply With Quote
 
Phill. W
Guest
Posts: n/a
 
      9th Dec 2003
"Doug Gifford" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
.. . .

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.


 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      9th Dec 2003
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

"Doug Gifford" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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?
>
>
>
>
>
>



 
Reply With Quote
 
Doug Gifford
Guest
Posts: n/a
 
      13th Dec 2003
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!
"Doug Gifford" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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?
>
>
>
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
try..catch catch the error only running inside of VS.NET Dragos Hilbert Microsoft C# .NET 2 11th Mar 2008 03:35 PM
Catch block is failing to catch exceptions when not run from MSDev CodeSlayer Microsoft C# .NET 2 16th Feb 2006 06:42 PM
Try Catch Question How Make it go to specific catch? needin4mation@gmail.com Microsoft C# .NET 3 10th Oct 2005 08:59 PM
Try and Catch Problem Brian Conway Microsoft C# .NET 2 25th May 2004 08:58 PM
a CATCH 22 problem with IE Kismet Windows XP Internet Explorer 1 30th Oct 2003 06:25 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:01 PM.