如何从Velocity模板脚本(VTL)中引发用户定义的异常? [英] How to throw an user-defined exception from Velocity Template Script (VTL)?

查看:105
本文介绍了如何从Velocity模板脚本(VTL)中引发用户定义的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to throw an user-defined exception from Velocity Template Script (VTL) ?

从我的Velocity脚本中,我需要根据条件引发异常,这样,呼叫者就可以捕获异常并向最终用户显示有用的错误消息。

From my velocity script, i need to throw an exception based on a condition, so that the caller can catch the exception and present an useful error messages to the end user.

例如。

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

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

In the above example, if passwordfield1 and passwordfield2 is not matching,an appropriate exception should be thrown and that needs to be propagated to the end-user.

有什么方法可以从速度脚本中实现这一目标吗?如果不是,请提出另一种方法。

Is there any way to achieve this from velocity script? If not, please suggest an alternate approach.

推荐答案

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

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

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

这篇关于如何从Velocity模板脚本(VTL)中引发用户定义的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆