Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Moteur des scripts: Exemple de gestion des exceptions


{filelink=939}

 
import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
 
public class ExceptionScript {
  public static void main(String[] args) {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("js");
    engine.put("a", 3);
    engine.put("b", 14);
    engine.put("c", 8);
 
    Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
    Object a = bindings.get("a");
    Object b = bindings.get("b");
    Object c = bindings.get("c");
    System.out.println("Valeur de a = " + a);
    System.out.println("Valeur de b = " + b);
    System.out.println("Valeur de c = " + c);
 
    Object resultat;
    try {
      resultat = engine.eval("x = (a + b)*d;");
      System.out.println("(a + b)*d= " + resultat);
    } catch (ScriptException e) {
      e.printStackTrace();
    }
 
  }
}
/*
  Exception Générée:
  javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "d" n'est pas défini (<Unknown source>#1) in <Unknown source> at line number 1
      at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:133)
      at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:149)
      at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
      at ExceptionScript.main(ExceptionScript.java:28)
  Caused by: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "d" n'est pas défini (<Unknown source>#1)
      at sun.org.mozilla.javascript.internal.ScriptRuntime.constructError(ScriptRuntime.java:3224)
      at sun.org.mozilla.javascript.internal.ScriptRuntime.constructError(ScriptRuntime.java:3214)
      at sun.org.mozilla.javascript.internal.ScriptRuntime.notFoundError(ScriptRuntime.java:3287)
      at sun.org.mozilla.javascript.internal.ScriptRuntime.name(ScriptRuntime.java:1559)
      at sun.org.mozilla.javascript.internal.Interpreter.interpretLoop(Interpreter.java:3150)
      at sun.org.mozilla.javascript.internal.Interpreter.interpret(Interpreter.java:2239)
      at sun.org.mozilla.javascript.internal.InterpretedFunction.call(InterpretedFunction.java:138)
      at sun.org.mozilla.javascript.internal.ContextFactory.doTopCall(ContextFactory.java:323)
      at sun.org.mozilla.javascript.internal.ScriptRuntime.doTopCall(ScriptRuntime.java:2747)
      at sun.org.mozilla.javascript.internal.InterpretedFunction.exec(InterpretedFunction.java:149)
      at sun.org.mozilla.javascript.internal.Context.evaluateReader(Context.java:1169)
      at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:123)
    ... 3 more
*/

A Voir sur le même Sujet:

  • rhinoscriptengine javascript execute shell script
  • referenceerror: "document" n'est pas défini javax.script
  • gerer les exceptions en perl : eval
  • comment lire InterpretedFunction javascript