I have this code on a simple "Export data to Excel" button for our end users of a certain Vision application:
try:
path = system.dataset.exportExcel(fileName,1,data,1)
except FileNotFoundError:
system.gui.errorBox("File is in use. Please save this file under a different name or close the existing file, then try again.", "Export Failure")
But when trying to export over a file that is in use, I can't get it to actually reach the "Except" block. It always throws an error while executing the system.dataset.exportExcel function, shown below:
You're catching a Python FileNotFoundError, not the Java FileNotFoundException. You can either catch bare java.lang.Exception, or import the specific error and catch it.
Another option is to separate the file choosing from the writing; system.file.saveFile will give you a path that you can then check whether a file exists at, and then manually write out your dataset with system.dataset.toExcel.
Thank you, that makes sense -- I am still learning how Jython works under the hood. I modified the script, and it should be working but it's still throwing the same error message instead of reaching any of these "test" except blocks. You can see I'm trying to import a few things to see what works, but none of them are working...
from java.lang import Throwable
from java.lang import Exception as JException1
from java.io import FileNotFoundException as JException2
data = event.source.parent.getComponent('MyTable').viewDataset
fileName = "MyReport.xlsx"
try:
path = system.dataset.exportExcel(fileName,1,data,1)
except Throwable, a:
print "A: ", a
except JException1, b:
print "B: ", b
except JException2, c:
print "C: ", c
If I try overwriting an existing file named MyReport.xlsx, I continue seeing the error that is pictured in the original post. Is it best practice to use the 'bytes' method instead of directly using system.dataset.exportExcel?
It's within an "actionPerformed" event handler script on a button in a Vision window. I'm looking at the output console for the "print" statements, and if all goes well, I will get the error box that I put in the original post.