Search in sources :

Example 1 with ThrowException

use of org.matheclipse.core.eval.exception.ThrowException in project symja_android_library by axkr.

the class ExprEvaluator method evalTryCatch.

public static IExpr evalTryCatch(final IExpr expr, EvalEngine[] engineRef) {
    EvalEngine engine = engineRef[0];
    EvalEngine.set(engine);
    // engine.reset() must be done before parsing step
    IExpr preRead = S.$PreRead.assignedValue();
    IExpr temp;
    try {
        if (preRead != null && preRead.isPresent()) {
            temp = engine.evaluate(F.unaryAST1(preRead, expr));
        } else {
            temp = engine.evaluate(expr);
        }
    } catch (ReturnException rex) {
        LOGGER.debug("ExprEvaluator.evalTryCatch() failed", rex);
        return rex.getValue();
    } catch (BreakException | ContinueException conex) {
        LOGGER.debug("ExprEvaluator.evalTryCatch() failed", conex);
        IAST ast = F.Continue();
        if (conex instanceof BreakException) {
            ast = F.Break();
        }
        // No enclosing For, While or Do found for `1`.
        IOFunctions.printMessage(S.Continue, "nofwd", F.list(ast), engine);
        temp = F.Hold(ast);
    } catch (ThrowException e) {
        LOGGER.debug("ExprEvaluator.evalTryCatch() failed", e);
        // Uncaught `1` returned to top level.
        IAST ast = F.Throw(e.getValue());
        IOFunctions.printMessage(S.Throw, "nocatch", F.list(ast), engine);
        temp = F.Hold(ast);
    } catch (IterationLimitExceeded e) {
        LOGGER.debug("ExprEvaluator.evalTryCatch() failed", e);
        // Iteration limit of `1` exceeded.
        int iterationLimit = engine.getIterationLimit();
        IOFunctions.printMessage(S.$IterationLimit, "itlim", F.list(iterationLimit < 0 ? F.CInfinity : F.ZZ(iterationLimit), expr), engine);
        temp = F.Hold(expr);
    } catch (RecursionLimitExceeded e) {
        LOGGER.debug("ExprEvaluator.evalTryCatch() failed", e);
        // Recursion depth of `1` exceeded during evaluation of `2`.
        int recursionLimit = engine.getRecursionLimit();
        IOFunctions.printMessage(S.$RecursionLimit, "reclim2", F.list(recursionLimit < 0 ? F.CInfinity : F.ZZ(recursionLimit), expr), engine);
        temp = F.Hold(expr);
    }
    if (!engine.isOutListDisabled()) {
        engine.addInOut(expr, temp);
    }
    return temp;
}
Also used : RecursionLimitExceeded(org.matheclipse.core.eval.exception.RecursionLimitExceeded) ThrowException(org.matheclipse.core.eval.exception.ThrowException) ContinueException(org.matheclipse.core.eval.exception.ContinueException) BreakException(org.matheclipse.core.eval.exception.BreakException) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) IterationLimitExceeded(org.matheclipse.core.eval.exception.IterationLimitExceeded) ReturnException(org.matheclipse.core.eval.exception.ReturnException)

Aggregations

BreakException (org.matheclipse.core.eval.exception.BreakException)1 ContinueException (org.matheclipse.core.eval.exception.ContinueException)1 IterationLimitExceeded (org.matheclipse.core.eval.exception.IterationLimitExceeded)1 RecursionLimitExceeded (org.matheclipse.core.eval.exception.RecursionLimitExceeded)1 ReturnException (org.matheclipse.core.eval.exception.ReturnException)1 ThrowException (org.matheclipse.core.eval.exception.ThrowException)1 IAST (org.matheclipse.core.interfaces.IAST)1 IExpr (org.matheclipse.core.interfaces.IExpr)1