5.3.2010 20:50, Santosh Tiwari kirjoitti:
Folks,

I am using jython (Pythoninterpreter) as an embedded script engine in my java program.

I want to capture the error in the user script (if any) and display it to the user. Sometimes, I can capture the error message, but sometimes I can't. To demonstrate my problem, I created the following simple example.

<code>
import org.python.core.PyException;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;

public class TestJython {

    public static void main(String[] args) {

        String sourceCode1 = "import sys\na=\n";
        String sourceCode2 = "import sys\nb=c\n";
        PythonInterpreter interp = new PythonInterpreter();

        //trying "a=", line, column, and message are printed
        try {
            interp.exec(sourceCode1);
        } catch (PyException excp) {
            excp.normalize();
            PyObject lineno = excp.value.__findattr__("lineno");
            PyObject offset = excp.value.__findattr__("offset");
            PyObject message = excp.value.__findattr__("msg");
            System.out.println("line number   = " + lineno);
            System.out.println("column number = " + offset);
            System.out.println("Error message = " + message);
            System.out.println("Exception message = " + excp.toString());
        }

        //trying "b=c", line, column, and message are null, why?
        try {
            interp.exec(sourceCode2);
        } catch (PyException excp) {
            excp.normalize();
            PyObject lineno = excp.value.__findattr__("lineno");
            PyObject offset = excp.value.__findattr__("offset");
            PyObject message = excp.value.__findattr__("msg");
            System.out.println("line number   = " + lineno);
            System.out.println("column number = " + offset);
            System.out.println("Error message = " + message);
            System.out.println("Exception message = " + excp.toString());
        }
    }
}
</code>

The above example executes two incorrect pieces of code. In the first instance, I can get the line number, column number, and the error message correctly. In the second instance, these values are null. Even if the values are null, the exception message still has the necessary information (as is demonstrated by its toString() output).

What can I do to get the line number, column number, and the error message correctly all the time?

The Jython interpreter does not fill in these fields at the moment. That is one of the problems I'd like to get addressed for 2.5.2.
The reason you are getting those in the first instance is probably because there is a syntax error there, and the code isn't even executed yet.
Thanks,



------------------------------------------------------------------------------ Download Intel&#174; Parallel Studio Eval Try the new software tools for yourself. Speed compiling, find bugs proactively, and fine-tune applications for parallel performance. See why Intel Parallel Studio got high marks during beta. http://p.sf.net/sfu/intel-sw-dev
_______________________________________________ Jython-users mailing list Jython-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jython-users