If Error Fails on Open File

It’s always been easy to handle a non-existent or otherwise missing file, but it seems that Pan X responds with its own error dialog before allowing my If Error to handle it.

OpenFile “XXX”
If Error
Message “No Such File.”
EndIf

According to the Help file that should work but it does not for me either. This, however, does work:

try
    OpenFile “XXX”
catch
    Message “No Such File.”
endcatch

Thanks Gary, of all the ways I tried I didn’t come up with that one. It carries one issue with it though in that with every run it opens a new Untitled database.

Yikes! I had not noticed that these files were being created.That definitely is another bug for sure.

This is definitely a bug. I have added it to bitbucket, the link is below.

In Panorama X the openfile statement is implemented as a custom statement, i.e. it is written in Panorama code. When I wrote that code, I forgot to add in error handling. I’ll have to be careful in doing that, as my first instinct would be to use try/catch. Looking at the code (which you can do also), it’s not obvious to me why an untitled database would be created, that may well be a separate bug I will have to look into.

The openfile statement can do several things – open a database, a text file, or append. If you just want to open a database, you should be able to use the opendatabase statement with if error, like this:

opendatabase "xyz"
if error
    message "nope"
endif

The reason this works is that opendatabase is a built in statement written in Objective-C, so if/else should work. It can work with custom statements also, but only if they explicitly trap and return the error.

Here’s the bitbucket link for this problem:

This has been happening for as long as I can remember. I have had lots of .initialize procedures that open an untitled window and stop. This explains it.

Here’s the problem. Suppose you have this statement:

openfile "xyz"

When this is encountered, Panorama

  • Checks to see if there is a file named xyz.pandb. If there is, it opens it.
  • Next, it checks to see if there is a file named xyz.pan, if so, it opens it.
  • If it doesn’t find either of those problems, it assumes that xyz is a text file, and it creates a new untitled database to load that into. Unfortunately, it doesn’t notice that the text file doesn’t exist until after it has done this. That’s where the untitled database is coming from.

I have changed the code so that it doesn’t create the untitled database until it verifies that a file actually exists. I have also enclosed the entire code with try/catch and if there is an error it is returned back to the caller (this is what try/catch was invented for). In other words, openfile now works with if error (I tested and verified that it now works).

In the meantime, while you are waiting for this fix to be released, you can either use the opendatabase statement (assuming you just want to open a database and not do one of the fancy other things that openfile can do (like append), or you can explicitly check to make sure the file exists before calling openfile.

If you aren’t aware that openfile and opendatabase are different, you might want to review the help pages for these statements. The opendatabase statement is not new in Panorama X, it goes back to at least Panorama 6 and maybe even back to Panorama 4.

I do see the benefit of the separate commands - and the issues of trying to have OpenFile cover everything. I’ll convert to OpenDatabase as I write new procedures and as I encounter it in existing procedures.

The Note in the OpenFile docs is helpful in referring us to the other options but of course, since I already knew it all, I didn’t read it previously.