exception - 如何从 Velocity 模板脚本 (VTL) 中抛出用户定义的异常?

标签 exception velocity user-defined vtl

如何从 Velocity 模板脚本 (VTL) 抛出用户定义的异常?

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

例如。

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

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

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

最佳答案

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

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

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

关于exception - 如何从 Velocity 模板脚本 (VTL) 中抛出用户定义的异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15659200/

相关文章:

c#:如何处理来自第 3 方库的终结器异常?

c++ - 台球游戏物理速度计算问题

java - Apache 速度 : Is there a standard way of verifying the correctness of a template from the command line?

java - EclipseLink JPA 2.1 用户提供的连接

c# - 刷新列表框项目时出现异常

java - ClassNotFoundException 即使包含该类的 jar 正确存在于类路径中

java - 如何在libGDX中以相反的方式射击子弹

java - 水壶使用PGP加密文件

android - 在 android 中分析用户定义的方法

c# - 从 catch block 中抛出嵌套异常……这明智吗?