FolderItem and Try

What is wrong with this Try?
BTW PFolderProp does exist but PFolderProp.child doesn’t

[code] Try
If (PFolderProp.child(“PrefsFile.ini”) <> Nil And PFolderProp.child(“PrefsFile.ini”).Exists) Then

  End If//If (IniHerd.ThColl.child("PrefsFile.ini") = Nil Or I
End Try

You got to address a specific exception.


Basically, you need to Dim e as the exception you see in the debugger.

Note that if you want your Try-Catch to work in the IDE, you need to uncheck “Break on exceptions” in the Project menu.

I now have [code] Try
If (PFolderProp.child(“PrefsFile.ini”) <> Nil And PFolderProp.child(“PrefsFile.ini”).Exists) Then

  End If//If (IniHerd.ThColl.child("PrefsFile.ini") = Nil Or I
Catch e as NilObjectException
End Try

Sorry but the Catch NilObjectexception isn’t being thrown and nothing is being thrown.

If PFolderProp is not nil and does exist (as you said in the original post) then the child will only be nil if the name that you are providing contains invalid characters (in which case it will throw an UnsupportedFormatException).

If FolderItem.Child returned nil when “PrefsFile.ini” doesn’t exist at that location, then you would never have an opportunity to create the file.

What are you expecting to happen with your block of code?

Xojo supports Short Circuit Evaluation.

i.e. this one-liner:

If (PFolderProp.child("PrefsFile.ini") <> Nil And PFolderProp.child("PrefsFile.ini").Exists) Then

is actually equivalent to this nested If statement:

If (PFolderProp.child("PrefsFile.ini") <> Nil Then If PFolderProp.child("PrefsFile.ini").Exists) Then

In other words, PFolderProp.child("PrefsFile.ini").Exists) only executes if (PFolderProp.child("PrefsFile.ini") <> Nil is True. Hence, no exception will be raised.

Thanks for terminology.
Okay. So I Changed it to ONLY

If PFolderProp.child("PrefsFile.ini").Exists Then

and still not a catch

Why do you expect a nilobject?

When you take a folderitem object that is not nil, such as PFolderProp
And get a folderitem from it using .child()

What you then have is a non-nil folderitem.
It doesnt have to exist, but it is not nil

There is no reason for an exception here.

The conditions of the IF statement aren’t accessing the file. They are merely testing for the possibility of the file and it’s parent folder. The tests are returning a Boolean result. If the file doesn’t exist, a NilObjectException will be thrown if you actually attempt to access the file.

Aah. I kept on getting Nil’s before later. Thanks. That’s what I tried when I actually went to use it, which is where the Try should be. Thanks. Testing choices

There is no exception to catch.

You are looking for something more like this:

[code]if PFolderProp <> nil and PFolderProp.Exists then

dim prefsFile as FolderItem = PFolderProp.Child(“PrefsFile.ini”)

if prefsFile.Exists then
// This is your opportunity to do something with the existing file (copy it or write to it, etc.)
// This is where you would handle the case where the file doesn’t exist (create it or bail out)


Jeff is correct here: PFolderProp is not Nil; but it does not exists so it fails at the .Exists test !

Check the code below in the debugger.

And, when checking for the Logic, if you do not created a file, it cannot exit ?

[code] Dim PFolderProp As FolderItem

PFolderProp = SpecialFolder.Documents.Child(“PrefsFile.ini”)

If PFolderProp <> Nil And PFolderProp.Exists Then
  MsgBox "It works !"
End If//If (IniHerd.ThColl.child("PrefsFile.ini") = Nil Or I

// CpyFles()
End Try[/code]

How do you create and write into your .ini file ?

As far as I understand the wikipedia contents (link above), it is a text file. So you will use TextInputStream / TextOutputStream.

Get an eye of the examples code shared there for real explanations.

I got it. Thanks

If PFolderProp.child("PrefsFile.ini").Exists Then Return True ElseIf PFolderProp.child("PrefsFile.ini").Exists = False Then

This is correct. Using: If Not PFolderProp.child("PrefsFile.ini").Exists works too.

Also, setting a Property to PFolderProp.child("PrefsFile.ini") and using that property is better:

My_PFolderProp = PFolderProp.child("PrefsFile.ini").Exists

Faster and less error prone.