Try not catching SQL Table Missing Error

Hi,
I'm running a series of named queries using scripting. I've put try-except statements around most of my named queries, but it doesn't appear to be catching the error from the actual named query e.g. Table is missing or Database is offline

	if C1_Database == True:
		print "Secondary Database Write Attempt"
		try:
			Write_Return = system.db.execQuery(Path,Parameters)
			print "Secondary Write Success"
		except Exception as error:
			print str(error)

It does catch errors like wrong path or wrong parameters

First, you should use system.db.execUpdate for an insert/update, not execQuery.

Second, you can try catching the java exception with something like this:

from java.lang import Throwable

try:
    Write_Return = system.db.execUpdate(Path, Parameters)
    print "Secondary Write Success"
except Throwable, error:
    system.util.getLogger("Database").error("Named query failed", error)

I had the query in question set to query not update changed that and didn't fix it but adding the throwable section did now i'm getting what i expected from the error handling.

Do you happen to know why I need the throwable code instead of a regular exception?

The exception is happening in java, not jython. You're going a layer deeper with this. Glad it's working for you now!

Ahh got you that makes sense

To be more precise, java exceptions and jython exceptions are not in the same class hierarchy, so an except clause that names an exception type cannot catch from the other hierarchy. You need two except clauses in some cases.

Oh thanks for the info i still need to protect against jython exceptions so knowing that is very helpful :slight_smile:

You should be able to use a single clause

except (Exception, Throwable) as e:

Yes, but then you cannot easily use the backtrace-handling logger methods. And if you are going to throw away the backtrace, what's the point of catching any generic exception?
If you aren't going to ignore the exception entirely (or expected exceptions for which you have specific handling), then you really want the proper backtrace.

If you want to handle some exceptions and then log the generic ones, use three or more except clauses. The last two should be the jython and java generics, and the jython generic should be converted to java for the logger.

High on my scripting wish list is extending LoggerEx methods to accept Jython throwables and transparently adapt their stacktrace to the right format. That way logging either branch of exception 'just works' (if you've structured your catch the right way).