How to throw a user-defined exception from the Velocity Script (VTL) template?

How to throw a user-defined exception from a Script (VTL) speed template?

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

Example.

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

In the above example, if passwordfield1 and passwordfield2 do not match, the appropriate exception should be thrown, which should be passed to the end user.

Is there a way to achieve this with a script speed? If not, suggest an alternative approach.

+4
source share
1 answer
 context.put("exceptionThrower", new ExceptionThrower()); public class ExceptionThrower { public void throwUserDefined() { throw new UserDefinedException(); } } #if ($whatever) $exceptionThrower.throwUserDefined() #else blah blah #end 
+2
source

All Articles