Handling Errors

The Python language provides error handling via the try...except code block. This can be used within scripts to trap exceptions and handle problems that would otherwise cause the script to terminate.

In the example script below, an attempt is made to retrieve a model from a IBM® SPSS® Collaboration and Deployment Services Repository. This operation can cause an exception to be thrown, for example, the repository login credentials might not have been set up correctly, or the repository path is wrong. In the script, this may cause a ModelerException to be thrown (all exceptions that are generated by IBM SPSS Modeler are derived from modeler.api.ModelerException).

import modeler.api

session = modeler.script.session()
try:
    repo = session.getRepository()
    m = repo.retrieveModel("/some-non-existent-path", None, None, True)
    # print goes to the Modeler UI script panel Debug tab
    print "Everything OK"
except modeler.api.ModelerException, e:
    print "An error occurred:", e.getMessage()
Note: Some scripting operations may cause standard Java exceptions to be thrown; these are not derived from ModelerException. In order to catch these exceptions, an additional except block can be used to catch all Java exceptions, for example:
import modeler.api

session = modeler.script.session()
try:
    repo = session.getRepository()
    m = repo.retrieveModel("/some-non-existent-path", None, None, True)
    # print goes to the Modeler UI script panel Debug tab
    print "Everything OK"
except modeler.api.ModelerException, e:
    print "An error occurred:", e.getMessage()
except java.lang.Exception, e:
    print "A Java exception occurred:", e.getMessage()