如何从Velocity Template Script(VTL)中抛出用户定义的异常?

时间:2013-03-27 12:48:13

标签: exception velocity user-defined vtl

如何从Velocity Template Script(VTL)中抛出用户定义的异常?

从我的速度脚本中,我需要根据条件抛出异常,以便调用者可以捕获异常并向最终用户提供有用的错误消息。

例如。

#if($passwordfield1 != $passwordfield2)
throw an exception here
#elseif($passwordfield1 == $passwordfield2)
do something
#end

在上面的示例中,如果passwordfield1和passwordfield2不匹配,则应抛出适当的异常,并且需要将其传播给最终用户。

有没有办法从速度脚本实现这一点?如果没有,请提出另一种方法。

1 个答案:

答案 0 :(得分:4)

context.put("exceptionThrower", new ExceptionThrower());

public class ExceptionThrower {
    public void throwUserDefined() {
        throw new UserDefinedException();
    }
}

#if ($whatever) 
$exceptionThrower.throwUserDefined()
#else
blah blah
#end