Throw()
Throws a developer-specified exception, which can be caught with a <cfcatch> tag
Throw( message=string, type=string, detail=string, errorcode=string, extendedInfo=string, object=any, cause=any );
Returns: void
Examples
// thrown as a statement example
try {
throw "thrown";
} catch (e){
dump(var=cfcatch, label="single argument keyword");
}
<span class="o">/*</span> <span class="nv">this</span> <span class="nv">won</span><span class="err">'</span><span class="nv">t</span> <span class="nv">work</span> <span class="nv">as</span> <span class="nv">expected</span><span class="p">,</span> <span class="nv">because</span> <span class="nv">thrown</span> <span class="nv">expects</span> <span class="nv">only</span> <span class="nv">a</span> <span class="nv">single</span>
<span class="nv">argument</span> <span class="nv">function</span> <span class="nv">in</span> <span class="nv">cfscript</span><span class="p">,</span> <span class="nv">only</span> <span class="nv">message</span> <span class="nv">will</span> <span class="nv">be</span> <span class="nv">populated</span>
<span class="nv">see</span> <span class="nv">https</span><span class="p">:</span><span class="c">//luceeserver.atlassian.net/browse/LDEV-2832</span>
<span class="o">*/</span>
<span class="nv">try</span> <span class="p">{</span>
<span class="nv">throw</span> <span class="nv">message</span><span class="o">=</span><span class="s2">"thrown"</span>
<span class="nv">detail</span><span class="o">=</span><span class="s2">"deets"</span>
<span class="nv">errorCode</span><span class="o">=</span><span class="s2">"403"</span>
<span class="nv">type</span><span class="o">=</span><span class="s2">"Test"</span><span class="p">;</span>
<span class="p">}</span> <span class="nf">catch</span> <span class="p">(</span><span class="nv">e</span><span class="p">){</span>
<span class="nf">dump</span><span class="p">(</span><span class="k">var</span><span class="o">=</span><span class="nv">cfcatch</span><span class="p">,</span> <span class="nv">label</span><span class="o">=</span><span class="s2">"additional arguments are ignored"</span><span class="p">);</span>
<span class="p">}</span>
<span class="c">// use this syntax instead</span>
<span class="nv">try</span> <span class="p">{</span>
<span class="nf">throw</span> <span class="p">(</span><span class="nv">message</span><span class="o">=</span><span class="s2">"thrown"</span><span class="p">,</span>
<span class="nv">detail</span><span class="o">=</span><span class="s2">"deets"</span><span class="p">,</span>
<span class="nv">errorCode</span><span class="o">=</span><span class="s2">"403"</span><span class="p">,</span>
<span class="nv">type</span><span class="o">=</span><span class="s2">"Test"</span><span class="p">);</span>
<span class="p">}</span> <span class="nf">catch</span> <span class="p">(</span><span class="nv">e</span><span class="p">){</span>
<span class="nf">dump</span><span class="p">(</span><span class="k">var</span><span class="o">=</span><span class="nv">cfcatch</span><span class="p">,</span> <span class="nv">label</span><span class="o">=</span><span class="s2">"script throw with arguments"</span><span class="p">);</span>
<span class="p">}</span>
See also
- <cfthrow>
- Search Issue Tracker
- Search Lucee Test Cases (good for further, detailed examples)