Java Reference
In-Depth Information
show them if a
ScriptException
is encountered. The
javax.tools.Diagnostic-
Collector
class provides a means to do this.
First, create an instance of
DiagnosticCollector
, then pass this to either the
compile()
or
eval()
methods on the
JavaFXScriptEngine
instance.
DiagnosticCollector
diags =
new DiagnosticCollector();
try {
CompiledScript compiled =
engine.compile(script,
diags
);
...
Now, when a
ScriptException
is thrown, detailed error messages are contained
in the
DiagnosticCollector
object. Listing 11.5 shows how to list all the error
messages.
Listing 11.5
Java Code - Java Scripting API Error Handling
} catch (ScriptException ex) {
List<Diagnostic> errorList =
diags.getDiagnostics();
Iterator<Diagnostic> iter =
errorList.iterator();
while (iter.hasNext()) {
Diagnostic d = iter.next();
System.out.println(
d.getKind().toString() + ": Line:" +
d.getLineNumber() + " Col:" +
d.getColumnNumber() + "\n'" +
d.getMessage(null) + "'");
}
}
To illustrate this, if we take the
Student
script and change the
getName()
function
to return the erroneous instance variable
nameBogus
. Without diagnostic han-
dling, we get the following fairly useless error message from
ScriptException
.
javax.script.ScriptException: compilation failed
at com.sun.tools.javafx.script.JavaFXScriptEngineImpl.
parse(JavaFXScriptEngineImpl.java:260)
at com.sun.tools.javafx.script.JavaFXScriptEngineImpl.
compile(JavaFXScriptEngineImpl.java:119)
at com.sun.tools.javafx.script.JavaFXScriptEngineImpl.
compile(JavaFXScriptEngineImpl.java:110)
at FXCompile.main(FXCompile.java:45)
Search WWH ::
Custom Search