Try Except FOREIGN KEY error

I have some code that’s wrapped in a try-except block.
In this try-except block, there is a query that fails and gives a FOREIGN KEY error.
When this error happens for some reason the code fails and doesn’t go to the except part.
This only happens when I use except Exception, e: instead of except:.
When using except: the code gets in the exception part.
So for now I’m using except: but I would love to know why except Exception, e: doesn’t work.

You are probably getting a java.lang.Expection instead of a python Expection raised by the system.db function.
try:
from java.lang import Exception

or maybe better to avoid it clashing with python:

from java.lang import Exception as JavaExpection
	try:
		system.db.runQuery('query')
	except (JavaExpection, Exception), e:		
		return str(e)
2 Likes

A post I wrote a while back about properly using try/except with Database restraints - Insert into database only if the entry doesn't exist - #2 by bkarabinchak.psi

2 Likes