[SOLVED]runPrepUpdate is failing to return a value

I had some working code:

try:
    db = 'xxyyzz'
    sql = 'INSERT INTO dbo.myTable (column1, column2, column3) VALUES (?,?,?)'
    args = [arg1, arg2, arg3]
    i = system.db.runPrepUpdate(sql, args, db)
    system.gui.messageBox('Rows updated: ' + str(i))
except Exception, e:
    system.gui.messageBox('error: ' + str(e))

This code used to work correctly each time but now my runPrepUpdate executes and then nothing ever happens. The code just ends. Even the try/except doesn’t execute.

Anyone know what is going on or have seen this before?

Is the data being inserted? If not, consider adding an except clause specifically for java.lang.Exception. An except clause for python’s Exception class won’t catch java exceptions.

Ok, I changed the code:

import java.lang.Exception
try:
    db = 'xxyyzz'
    sql = 'INSERT INTO dbo.myTable (column1, column2, column3) VALUES (?,?,?)'
    args = [arg1, arg2, arg3]
    i = system.db.runPrepUpdate(sql, args, db)
    system.gui.messageBox('Rows updated: ' + str(i))
except java.lang.Exception, e:
    system.gui.messageBox('error: ' + str(e))

which then gave me an error:

Error executing sytem.db.runPrepUpdate...

It doesn’t seem to give me any useful information other than the insert fails but now I’m thinking it’s because I keep inserting the same PRIMARY KEY over and over.

I want to say that before it was returning an error for this and not just stopping without an error but I’m not 100% sure.

If I want to catch these types of errors do I always need to use java.lang.exception? What are Exception, e errors for then?

The Exception as e is the very generic error catch for Python 3.x. Not all error are programmed to raise the generic Exception for python, and for various reason.

Here’s a good read on the subject Python Exception.

1 Like

Python’s Exception classes inherit from PyObject, while java exceptions inherit from java.lang.Throwable. They simply don’t have a common parent class, so can only be caught in the same except clause when you specify no exception class at all. If you need to catch them both with the explicit syntax, use two except clauses.

See also:

Also note that java exceptions can have a chain of “causes” you can follow for more detail. Though some of that detail is lost in serialization when exceptions are reported from gateway to client.

2 Likes

Is this the proper syntax to catch both?

import java.lang.Exception
try:
    db = 'xxyyzz'
    sql = 'INSERT INTO dbo.myTable (column1, column2, column3) VALUES (?,?,?)'
    args = [arg1, arg2, arg3]
    i = system.db.runPrepUpdate(sql, args, db)
    system.gui.messageBox('Rows updated: ' + str(i))
except java.lang.Exception, e:
    system.gui.messageBox('JAVA ERROR: ' + str(e))
except Exception, e:
    system.gui.messageBox('PYTHON ERROR: ' + str(e))

If you get impatient, just comment out try/except and let it throw an error, then you can see what the error is.