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?

Thanks,