How to get the line number of an error when calling Kotlin from Java?

781 Views Asked by At

I'm trying to get which line is generating an error when calling a kotlin script from java.

The following code gives line number -1.

try
{
    String scriptcode = "\n\nthrow Exception()";
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByExtension("kts");
    engine.eval(scriptcode);
}
catch(ScriptException se)
{
    System.out.println(se.getLineNumber());
}

Am I missing something?

1

There are 1 best solutions below

2
On

Could please try something like this. se.getStackTrace()[0].getLineNumber();