Exception handling

Hi, guys,

I have the following code that reads variables in the software and allows the user to save them into a text file.


Dim t As TextInputStream
Dim f As FolderItem
Dim dlg As OpenDialog

dlg = New OpenDialog

dlg.InitialDirectory=SpecialFolder.Userhome.Child("Dropbox")


f = dlg.ShowModal() // open open dialog for selecting a file to load

try
  
  if f<> nil then
    t = f.OpenAsTextFile // open selected text file
    
    
   // some code is here 
   
    t.Close
  else
    //user cancelled
  end
  
   
  
end try

Return True

The code works as expected except of the situation when the user tries to save the file with the name of the existing file.

The OS tells me that a file with this name already exists, and asks if I want to override it. If I choose to override, it gives me an error.

The following line throws an exception error


   t = f.OpenAsTextFile // open selected text file

I would appreciate any suggestion how to handle this situation better.

Thank you in advance,

Val

The OS asks but it doesnt automatically replace the file for you
You could delete the old one
Plus a text input stream is for READING the file. Not writing it. Did you mean to read it in first ?

Hi, Norman,

Thank you for pointing out that the code is to read the file. Actually, I posted a wrong piece of code.

The correct one is below


Dim t As BinaryStream
Dim s as String
Dim f as FolderItem
Dim dlg as New SaveAsDialog

dlg.InitialDirectory=SpecialFolder.Userhome.Child("Dropbox")

f = dlg.ShowModal() // open open dialog for selecting a file to load

try
  
  
  
  if f<> nil then
    t = BinaryStream.Create(f)
    
    
    //some code
    
    t.Write (s)
    t.Close
    
  else
    //user cancelled
  end
  
  
  
end try
Return True

The software freezes when the OS tries to replace the existing file with the new one.

Thank you for pointing that the original code READS the file instead of WRITING to it.

What is the best way to address this issue?

Thank you,

Val

Looking for a solution to the above problem, I found this code sample in the Language Reference.


Var dlg As New SaveAsDialog
Var f As FolderItem
dlg.InitialDirectory = SpecialFolder.Documents
dlg.PromptText = "Prompt Text"
dlg.SuggestedFileName = "Suggested Filename"
dlg.Title = "Title Property"
dlg.Filter = FileTypes1.Text // defined as a file type in FileTypes1 file type set
f = dlg.ShowModal
If f <> Nil Then
  // file saved
Else
  // user canceled
End If

Unfortunately, this code from the Language Reference does not compile. There is an error generated by the line


dlg.Filter = FileTypes1.Text // defined as a file type in FileTypes1 file type set

After removing this line, the code compiles but and application with the sample code placed into a BevelButton does not save the file.

I am not sure what is wrong.

Did you get that here:

http://documentation.xojo.com/api/files/folderitem.htmlDialog

If so, did you click on the Filter property in the list of properties, and see what that had to say?

In short, you’ll need to poke around a bit and put all the bits together.

recall that the dialog and the prompt from the OS does NOT actually DELETE the file
It just asks you if its ok to overwrite it

so your code simply tries to create a new one but the old one still exists

you need to delete the old file first
something like

Dim t As BinaryStream
Dim s as String
Dim f as FolderItem
Dim dlg as New SaveAsDialog

dlg.InitialDirectory=SpecialFolder.Userhome.Child("Dropbox")

f = dlg.ShowModal()
try
   
  if f<> nil then
  if f.exists then // <<<<<<<<<<<<<<<< !!!!!!!!!!!!!!!!!!!!!
       f.delete // remove the file if it existed
  end if
  t = BinaryStream.Create(f)

    //some code
    
    t.Write (s)
    t.Close
    
  else
    //user cancelled
  end
end try
Return True

Norman, thank you for the reply.

Deleting the existing file solved the problem. Thank you

Tim, you might not believe me but I poked around. At the link that you have provided, click the Filter property. There is a code sample there (see below)


Var dlg As New OpenDialog
dlg.Title = "Select an mp4 file"
dlg.Filter = FileTypes1.VideoMp4 // defined in the File Types Editor...

Var f As FolderItem = dlg.ShowModal
If f <> Nil Then
  MoviePlayer1.movie = Movie.Open(f)
Else
  // user cancelled
End If

For your convenience, I copied the code into the message.

Put this code into a XOJO project and try to compile it. It does not compile!

Is it FileTypes1 (the digit “1” in the end) or is it FileTypesl (letter “L” lower case in the end) - I tried both versions - none of them compiles.

Because unmodified code from the Language Reference does not compile, I asked the question at the forum.

This is not the only example of non-working code in the Language Reference.

What version are you using ?
The online wiki reference is ALWAYS for the most recent version and that could be part of the issue
(I’ll admit I havent tried the code)

Hi, Norman,

I am using the most recent XOJO 2019 Release 3 on a Mac.

I appreciate your help.

Val

ah you shouild file a bug report about that code not working then as doc code should work or get fixed so it does

Hi, Norman, thank you for all your help.

One more quick question - I’ve done this several years ago but forgot how to do it and cannot find the old file.

I have a three-column checklist. I want to export it into a text delimited file. I have a compiled file that does this job but I do not remember the code I used.

Any suggestions how to do it? I found this example but when the text file it created is imported into the ListBox in the other program - the ListBox does not look like the one that is used to create the file.

Dim f As FolderItem
Dim tisx As TextOutputStream
f = SpecialFolder.Desktop.Child("item.txt")
tisx = f.CreateTextFile
Dim Last_first_word As String
Dim lastName As String
Dim maxRow As Integer = Listbox1.ListCount - 1
For row As Integer =  0 To maxRow
  If Listbox1.Cell(row, 0) <> Last_first_word Then
    If lastName <> "" Then tisx.WriteLine(lastName)
 
    tisx.WriteLine("")
    tisx.WriteLine(ListBox1.Cell(row, 0))
    tisx.WriteLine(ListBox1.Cell(row, 1) + " " + ListBox1.Cell(row, 2))
    Last_first_word = ListBox1.Cell(row, 0)
    lastName = ListBox1.Cell(row, 3)
  Else
    tisx.WriteLine(ListBox1.Cell(row, 1) + " " + ListBox1.Cell(row, 2))
  End If
Next
 
If lastName <> "" Then tisx.WriteLine(lastName)
 
tisx.Close

Thank you in advance.

Val

[quote=470833:@Val Kozmenko]Is it FileTypes1 (the digit “1” in the end) or is it FileTypesl (letter “L” lower case in the end) - I tried both versions - none of them compiles.

Because unmodified code from the Language Reference does not compile, I asked the question at the forum.[/quote]
Did you create a filetype named FileTypes1? And did you add a type named VideoMp4? The example assumes you have already done so.

every time you call WRiteLine it writes one line
and that wont result in many values on one line seperated by tabs commas or anything else
just a bunch of lines

but I’m not sure from this code what _ should be written out

when you want to write a value followed by more values is Write
when you want to end the line and start another use WriteLine
you can write a tab character as either Chrb(9) or Encodings.ASCII.Chr(9)

Here where I am with this code. Pretty close … just need to find a way to fix an error within “for … Next” loop


Dim t As TextOutputStream
Dim s as String
Var f as FolderItem
Dim i As Integer

f=SpecialFolder.Desktop.Child("Text.txt")
//f.Delete

if f<> Nil Then
  
  t =TextOutputStream.Create(f)
  
  for row as Integer=0 to i
    
    t=WriteLine(ListBox1.Cell(row,0)) +Chrb(9) + (ListBox1.Cell(row,1)) + Chrb(9) +  (ListBox1.Cell(row,2))
  Next
  
  t.Close
end

This line is causing an error


t=WriteLine(ListBox1.Cell(row,0)) + "," +  (ListBox1.Cell(row,1)) + "," + (ListBox1.Cell(row,2))

Will keep working on this.

    t=WriteLine(ListBox1.Cell(row,0)) +Chrb(9) + (ListBox1.Cell(row,1)) + Chrb(9) +  (ListBox1.Cell(row,2))

typo ?

not T = writeline

but t.writeline