jython and java exceptions

Alan Kennedy alanmk at hotmail.com
Wed Aug 11 11:31:05 EDT 2004


[Jan Gregor]
 > There's result - i put aa on input to force error:

What execution environment are you running it in when you have this 
problem? A servlet container? Eclipse? From the command line?

I'm beginning to suspect some form of classloader/classpath issue.

 > Exc com.sybase.jdbc2.jdbc.SybSQLException: Stored procedure 'aa' not
 > found. Specify owner.objectname or use sp_help to check whether the
 > object exists (sp_help may produce lots of output).
 >  != subclass of java.sql.SQLException!

So your jython code finds that the exception raised does not subclass 
the base exception it is trying to catch. Which could mean that the 
definitions of the base exception class may be different between the 
code trying to catch the exception (your jython) and the code trying to 
raise it (Sybase jdbc).

Try the following experiments to see if you get behaviour that is different

1. Try this jython

import java.sql

try:
   raise java.sql.SQLWarning() # known subclass of SQLException
except java.sql.SQLException:
   print "Successfully caught"
else:
   print "Big trouble in Little China"

2. Write some similar code to the above in java, raising an SQLWarning 
when an object is created, e.g.

TestSubclassExceptions.java <---------->
import java.sql;

public TestSubclassExceptions
   {
   public TestSubclassExceptions ( )
     throws SQLWarning
     {
     throw new SQLWarning();
     }
   }
<-------------------------------------->

Making sure that the class definition is on your classpath, instantiate 
a TestSubclassExceptions object inside a jython try..catch as in #1, and 
see what happens.

3. Write a version of the above class that raises 
com.sybase.jdbc2.jdbc.SybSQLException instead. Make sure to run this one 
from the command line, so that you know that the jython classloader is 
the one loading the class and exception definitions.

If you're running inside some form of "enterprise container", you may be 
tripping over a classloader issue, where base classes/exceptions are 
being loaded by different classloaders, possibly with different security 
settings. Solving these problems is almost always a case of finding the 
right library directory to put your jython.jar, etc, in. This can 
sometimes require extensive documentation delving.

Final sanity check: are you setting the "python.home" property if/when 
you are embedding jython? There are some minor complexities associated 
with caching of "compiled" java packages. These can cause odd behaviour 
if you are executing your code from two different execution 
environments, e.g. debugging on a command line, and then also running in 
a servlet container.

If none of the above sheds any light, please post back with the details 
of the software you're using, and the locations in which you've placed 
your jython.jar, your sybase jdbc jars, etc.

 > Problem is that SybSQLException IS subclass of SQLException - verified
 > by Eclipse IDE (type hierarchy). Similar effects were with jdbc driver
 > for PostgreSQL.
 >
 > There's result:
 > Exc org.postgresql.util.PSQLException: ERROR: syntax error at or near
 > "aa"
 >  != subclass of java.sql.SQLException!

So it's definitely not a Sybase specific problem.

-- 
alan kennedy
------------------------------------------------------
email alan:              http://xhaus.com/contact/alan



More information about the Python-list mailing list