Try...Catch problems

Alright, it’s time to ask the question; What’s wrong with this code?

try
While Not checkFile.EOF
itemsArray.Append(checkFile.ReadLine)
Catch e as OutOfBoundsException
MsgBox(“Exceeded size of Array.”)
Wend
End Try

I’m following the example in the Docs (hopefully), but it’s throwing two errors:

Syntax error
Catch e as OutOfBoundsException

Syntax error
Wend

Thanks!

Put the While and Wend outside of the Try/EndTry.

That was it. Funny thing is though, in the Docs, all the code is inside the Try/End Try

Try
Dim d As Xojo.Core.Date
Dim t As Text = d.ToText
Catch e As RuntimeException
If e IsA NilObjectException Then
’ your code
Else
’ Re-raise the exception for the framework
Raise e
End If
End Try

Try
Dim a(5) As Integer
a(6) = 45 ’ Raises an OutOfBoundsException
Catch e As OutOfBoundsException
’ The above exception is caught here for you to handle
ErrorLabel.Text = “Exceeded size of array”
End Try

Thanks, Greg!
regards,
-Rob

You might want to look at using Do/Loop rather than While/Wend http://developer.xojo.com/do-loop which allows you to test at the beginning (and end) of the loop.

Do Until CheckFile.EOF itemsArray.Append(checkFile.ReadLine) Loop

You should not “catching” external errors INSIDE the loop.

Move the test to where it belongs. Try something like:

While Not checkFile.EOF
  Try
     itemsArray.Append(checkFile.ReadLine) // Exploded?
  Catch e as OutOfBoundsException // let's take care of it and read another one
     MsgBox("Exceeded size of Array.")
  End
Wend

Another way could be catching outside (you “try” the entire loop):

Try
  While Not checkFile.EOF
     itemsArray.Append(checkFile.ReadLine) // Exploded?
  Wend
Catch e as OutOfBoundsException // let's take care of it and finish/give up the loop
    MsgBox("Exceeded size of Array.")
End