Yes Sir, I tried using a new instance every time, but that did not help.

What I have noticed is: when there is syntax error ("SyntaxError"), it correctly captures the line number and other fields.

When there is name error ("NameError"), it does not capture the fields. They are still null. Here are the two error messages.

<SyntaxError>
line number   = 2
column number = 1
Error message = no viable alternative at input '='
Exception message =   File "<string>", line 2
    a=
    ^
SyntaxError: no viable alternative at input '='
</SyntaxError>



<NameError>
line number   = null
column number = null
Error message = null
Exception message = Traceback (most recent call last):
  File "<string>", line 2, in <module>
NameError: name 'c' is not defined
</NameError>


Now, when I print the exception message (toString()), it prints the correct info. What it means that the exception message has all the information I need. I just need to be able to extract it. I am looking at the toString() method to figure out if I can replicate it in my code (since I do not have access to all the fields in the exception, it is a little difficult).



On Fri, Mar 5, 2010 at 2:38 PM, Rafael Sierra <rafaeljsg14@gmail.com> wrote:
On Fri, Mar 5, 2010 at 3:50 PM, Santosh Tiwari <tiwaris@gmail.com> wrote:
> 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>

Did you tried with a new PythonInterpreter instance? (intr1 and intr2)

>
> 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,
>
>
>
>
> ------------------------------------------------------------------------------
> 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
>
>



--
Rafael Sierra
http://blog.stiod.com



--
Santosh Tiwari
tiwaris@gmail.com