Behavior of system.dataset.exportExcel

I have this code on a simple "Export data to Excel" button for our end users of a certain Vision application:

	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:

Is there anything that I can do to make the try/except logic work as intended?

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.

1 Like

Jython's normal exceptions don't cover exceptions generated by Java. Use code construct like this (preferably in a project library):

from java.lang import Throwable

def someFunction(someDataset, someFile):
		bytes = system.dataset.toExcel(someDataset)
		system.file.write(someFile, bytes)
	except Exception, e:
		print "Jython Exception", e
	except Throwable, t:
		print "Java Exception", t

I normally use loggers, though, and use PythonAsJavaException() (from to convert python exceptions to a java format that is logger-friendly.

1 Like

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... :thinking:

from java.lang import Throwable
from java.lang import Exception as JException1
from import FileNotFoundException as JException2

data = event.source.parent.getComponent('MyTable').viewDataset
fileName = "MyReport.xlsx"

	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?

Where are you executing this script and where are you looking for the output?

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.

I suppose I wasn't clear. Are you testing in a true client or the designer?

Both! Same behavior happens either way