6

如何从 Velocity 模板脚本 (VTL) 中引发用户定义的异常?

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

例如。

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

在上面的示例中,如果密码字段 1 和密码字段 2 不匹配,则应引发适当的异常并需要将其传播给最终用户。

有没有办法从速度脚本中实现这一点?如果不是,请建议另一种方法。

4

1 回答 1

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

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

#if ($whatever) 
$exceptionThrower.throwUserDefined()
#else
blah blah
#end
于 2013-03-27T15:31:10.830 回答