Reading Streams, Catching Exceptions, Can you?

BlitzMax Forums/BlitzMax Programming/Reading Streams, Catching Exceptions, Can you?

Rhodesy(Posted 2009) [#1]
Hello all,

Am I under the wrong impression that you can catch exceptions from reading streams when you read over the stream length?

IE,

I'm "ReadStream" 'ing a file, then I am doing ReadString(file,100000)

e.g.
try
  file = ReadStream(file)
  data = ReadString(file,100000)
catch e:Object
  print e.toString()
end try

but, say the size of the stream is only 100, should this be throwing an exception? because at the moment its just completely crashing :(

or, should I be doing it another way?


beanage(Posted 2009) [#2]
I had this problem designing a parser, till i got the following as the perfect (?) solution:

Local MyStreamString:String
Local MyStream:TStream

While Not Eof( MyStream )
  MyStreamString:+ReadLine( MyStream )
Wend


(edit:) propably not the direct answer on your q, but that way you wont even need to read beyound the streams size!


Rhodesy(Posted 2009) [#3]
I too thought of that, my concern is how it could affect the speed of the application. I'm trying to write something that will scan "a lot" of files (10,000+), so i'm trying to not use the while not eof method as a concern it would slow down.


BlitzSupport(Posted 2009) [#4]
Rhodesy, I did what you're trying to do (ie. use exceptions to catch stream errors) here:

Retrieve image information without loading entire image

Just look at the Try/Catch/End Try stuff in the first function. I used it to deal with local files, files being read and interrupted over the internet, broken image files, etc, and it did the job perfectly.

I didn't really run any timing tests before and after, but it didn't 'feel' like it took significantly longer when running through nearly 12,000 image files (3.4 GB).


Rhodesy(Posted 2009) [#5]
No matter what I try, it just doesn't want to work. :(


Otus(Posted 2009) [#6]
What is the problem exactly? When I run this it prints "Caught", as expected:
SuperStrict
Framework BRL.StandardIO
Import BRL.FileSystem

Local file:TStream = ReadFile("empty.txt")

Try
	Local data:String = ReadString(file, 100000)
Catch e:Object
	Print "Caught"
End Try