Trying to catch an exception

Hi,

When I import an appropriate file into the ListBox everything works well.

When I try to import a wrong file, it throws an exception and the app crashes. the t=TextInputStreamOpen(f) is highlighted as a point of error.


Dim t As TextInputStream
Dim f As FolderItem
Dim dlg As OpenDialog
Dim s As string
Dim anArray() As String
Dim d As New Date


dlg = New OpenDialog

dlg.Title = "OPEN CHECKLIST ..."

f = dlg.showModalWithin(Window1)


if f<> Nil Then
  s=f.Name
  anArray = s.Split(".")
  Window1.Title=anArray(0)
else
  Window1.Title=""
end

Window1.Listbox1.RemoveAllRows


Dim rowFromFile,oneCell As String
Dim i As Integer
If f <> Nil Then
  t =TextInputStream.Open(f)
  t.Encoding=Encodings.MacRoman //strings are MacRoman
  Do
    rowFromFile=t.ReadLine
    
    Window1.ListBox1.AddRow NthField(rowFromFile,Chr(9),1)
    For i=1 to CountFields(rowFromFile,Chr(9))
      oneCell=NthField(rowFromFile,Chr(9),i)
      Window1.ListBox1.Cell(Window1.ListBox1.ListCount-1,i-1)=oneCell
      Window1.ListBox1.CellType(Window1.ListBox1.LastIndex,0)=ListBox.TypeCheckBox
    Next
  Loop Until t.EOF
  t.Close
  
End If

Do you have any suggestions?

Don’t import a wrong file. :slight_smile:

But joking aside, if the line

t =TextInputStream.Open(f)

generates an error, you need to test before then whether f.exists = true
It is not enough for a folderitem to be ‘not nil’
You need to check that it exists also.

The file you selected might be a shortcut rather than the file itself.
It may be zero bytes in length.

If it exists and still causes an error, check whether it is being held open by something like Excel.

In what way is the file "wrong’
If it is not formatted the way you expect, that may be the problem. It may be possible to limit the type of file you see in the viewing dialogue.

dlg.showModalWithin(Window1)

Dear Jeff and Philip,

The app is a checklist application. To use it, a user needs to load a checklist that is saved as a text file (.txt). Each line in the text file becomes a line in the listbox with a checkbox in each line.

I found this bug when I mistakenly tried to load an rtfd (Rich Text File with Attachments) created by the Bean word processor.

I am not sure what is so special about the RTFD format because the app does not crash when I on purpose load an image or PDF into the checklist.

I will need to find a way to limit the dialog to show only .txt files as Philip suggested.

I will need to learn again how to do it. I’ve done it once in the past but I’ve forgotten.

Thank you again for your help, guys.

Val

dlg.Filter is the way to limit types of files.

you can add/insert a FileTypeGroup1 on the left and at use = FileTypeGroup1.All

You will find an appropriate example here:
https://documentation.xojo.com/api/files/textinputstream.html

To answer your original question, you can add a

Try-Catch block around your code to catch exceptions.

That particular line could result in one of two exceptions: NilObjectException or an IOException.

To use:

Try
    // your code here
Catch ex as NilObjectException
    // handle nil 
Catch ex as IOException
    // handle code. This will be things like 
    // file not readable or does not exist
End Try
2 Likes

Thank you, Greg.

RTFD is a package format, which is basically a folder that looks and smells like a file, but it isn’t a file. Check the .isFolder property of the folderitem.

1 Like