Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 772
anuu_online Avatar asked Oct 05 '22 14:10

anuu_online


1 Answers

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

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

#if ($whatever) 
$exceptionThrower.throwUserDefined()
#else
blah blah
#end
like image 112
Nathan Bubna Avatar answered Oct 10 '22 09:10

Nathan Bubna