The infinite story of the IOException

I still suffer from an IOExcception in a project.

I spend the while afternoon working on files using both the standalone application and running in the IDE (thing I stopped some weeks ago).

Sometimes I get IOExcception working with a file just doing a load / save as… then some Copy / Paste (string / Rows), Insert Rows, Changing the Listbox Headings. Sometimes I can deal with two or three files without troubles.

I do not found any shema to report in a Feddback entry.

Right now, I add the Nil checking in the if block from the code below:

[code] If ExportCSVFI = Nil Then
MsgBox “IOException quest” + EndOfLine + EndOfLine +_
“Did I found the source of the error ?” + EndOfLine +_
“The OS File Reference is Nil.”
End If

ExportTOS = TextOutputStream.Create(ExportCSVFI)
If ExportTOS = Nil Then[/code]

The last IOErrorException stopped in the ExportTOS = TextOutputStream.Create(ExportCSVFI) line.

ExportCSVFI is not Nil.

BTW: I already reported a strange behavior with Text xxx Stream / BinaryStream: now, I show you what I get in the Save As… dialog:

Notice the non folder entries (csv files): they are non Folder items which is normally impossible: clearly a bug (bug who can be the bug owner ?). The file I was trying to save is the one ending with -b.csv.
Boring, but for me just an annoyance: I copy the whole Listbox contents, quit the application, run it, Paste the data and Export them (I just have to remember to not copy anithing during the operations…). But the application cannot be sold in this state.

Reader: if you have an idea, share it please.

PS: I save the data in csv files, but when I do that in txt, I can have the same IOException error. The code to export the data is located in two different Methods.

Did you check the ErrorCode of the IOException?


normally this error might occur if

  • the path in ExportCSVFI is invalid (bad path name, or points to an offline / remote device)
  • the file at the path is otherwise occupied (already open / locked)
  • the path is valid, but there are no permissions granted

but as Tim said, capture the errorcode/message, it might be more informative than you think

and I think you meant INFINITE, not Indefinite :slight_smile:

Tim, Dave: thank for your answers.

I understand your answers, but not how I trap the error code / message.

Or is it in the debugger at crash time ?

Certainly. I think Indefinite is a mixt of two or three words: bad nap !

look in the Lang Ref for TRY/CATCH

OK: I may be tired : eyes full of dreams… I will go to bed and let the night do its job / go back here tomorrow.

Thank you all.

BTW: I’m lucky, I got the IOException. The LastErrorCode is -107

I noticed the FolderItem have as name my file name (that ends with the file extension: csv) and NativePath displays the file parent ! (if this is an error, of course).
It is not a directory.
LastErrorCode: 101

…and the IOExceptionError does not appears ! I falled here because I put a brakpoint in the If .LastErrorCode <> 0 Then

I have it open in front of my eyes :frowning:

Thank you guys, I think I really have to stop now: I stopped the application run before saving the data changes. It is really time to sleep.

I had enough of this… So, I took a sample code (below), changed ansi with UTF8 and placed a text instead of a TextField1 I do not wanted to add. Below is the used code:

[code] Dim Documents As FolderItem = SpecialFolder.Documents
If Documents <> Nil Then
Dim f As FolderItem = Documents.Child(“Sample.txt”)
If f <> Nil Then
//TextOutputStream.Create raises an IOException if it can’t open the file for some reason.
Dim t As TextOutputStream = TextOutputStream.Create(f)
t.Write(ConvertEncoding(“Essai de IOException Workaround”, Encodings.UTF8)) // Changes here
t = Nil
Catch e As IOException
End Try
End If
End If

Return True[/code]

The old code is still below the one above, but not executed (due to the Return True).

The initial run leads to the creation of the file with my text inside and no error at all.

Conclusion: I will refactor my code, the part that deals with Getting a FolderItem and a TextOutputStream / keep the part that write into the TextOutputStream, and with some good luck, this whole mess will be over.

Thank you all for the good will and help.

Good news:

I replaced the line from the code above [t.Write(ConvertEncoding(etc.))] by a call to a method where I copied the write to disk (using t.Write…) code I used previously, with minor adaptations and I got my data in my format.

This means that the IOException comes from my code.

Work to do: add the SaveAsDialog and if it works (why not ?), do something else / forget all about that !

Many thanks to all.

Just FYI, the TextInputStream, TextOutputStream and BinaryStream creation methods all throw exceptions on error now. It would be good practice to wrap your code in a try-catch block as Dave suggested whenever using these to allow your app to handle this more gracefully.

Thank you Greg.

On Xojo 2015r1, the provided code for TextOutputStream does not have a try-catch code. Even if I knew I’d better do that at the time I create this Menu Handler, it seems that I simply forgot to do so.

Note for the reader: Using a SQLite data base (and certainly the other db), this trouble does not exist.

I am quite sure now that sometimes I write code automatically (without thinking) and if it works fine once, twice, … for some days / weeks (for whatever reason), it’s OK for me (until I‘m starting to be in trouble). With this, I paid for that. :frowning: