#throw() an error from Velocity to emergency-stop a send

A terrifyingly long time ago — before some of you had your first job in martech — I wrote about Velocity “Poison Pills” for overly inclusive Smart Lists. That is, forcing a Java-level error so an email errors out instead of sending.

Brute force, but it works.

Someone asked me about that technique the other day, so I decided to rewrite my “throw an exception” Velocimacro so it shows better info in the Activity Log.

I now recommend #throw_v2:

#macro( throw_v2 $message )
#if( !$message.isEmpty() )
#evaluate( "${esc.h}set( ${message} )" )
#end
#end

You can include the macro definition in your global Velocity {{my.velocity includes}} token. (As I always say, you do have global includes, right? ☺)

Then call it like so:

#throw_v2("ineligible")

Unlike the earlier macro, this one lets you pass a custom message which will show up in the Activity Log for the Email Bounced Soft:

Why it works

#set("blah") is invalid Velocity, guaranteed to throw an error. By using #evaluate and wrapping it in an isEmpty() check, we delay the error until the macro is run (the macro will still compile so it’s available to other scripts).