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

二次信任 提交于 2019-12-07 07:46:18

问题


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

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.

For Example.

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

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.


回答1:


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

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

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


来源:https://stackoverflow.com/questions/15659200/how-to-throw-an-user-defined-exception-from-velocity-template-script-vtl

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