Catch exception thrown by custom function in JEXL

允我心安 提交于 2019-12-12 01:23:11

问题


I added some functions to the JEXL engine wich can be used in the JEXL expressions:

Map<String, Object> functions = new HashMap<String, Object>();

mFunctions = new ConstraintFunctions();
functions.put(null, mFunctions);
mEngine.setFunctions(functions);

However, some functions can throw exceptions, for example:

public String chosen(String questionId) throws NoAnswerException {
        Question<?> question = mQuestionMap.get(questionId);
        SingleSelectAnswer<?> answer = (SingleSelectAnswer<?>) question.getAnswer();
        if (answer == null) {
            throw new NoAnswerException(question);
        }
        return answer.getValue().getId();
}

The custom function is called when i interpret an expression. The expression of course holds a call to this function:

String expression = "chosen('qID')";
Expression jexl = mEngine.createExpression(expression);
String questionId = (String) mExpression.evaluate(mJexlContext);

Unfortunetaly, when this function is called in course of interpretation, if it throws the NoAnswerException, the interpreter does not propagete it to me, but throws a general JEXLException. Is there any way to catch exceptions from custom functions? I use the apache commons JEXL engine for this, which is used as a library jar in my project.


回答1:


After some investigation, i found an easy solution!

When an exception is thrown in a custom function, JEXL will throw a general JEXLException. However, it smartly wraps the original exception in the JEXLException, as it's cause in particular. So if we want to catch the original, we can write something like this:

try {
    String questionId = (String) mExpression.evaluate(mJexlContext);
} catch (JexlException e) {
    Exception original = e.getCause();
    // do something with the original
}


来源:https://stackoverflow.com/questions/20030241/catch-exception-thrown-by-custom-function-in-jexl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!