Whatever message this page gives is out now! Go check it out!

Throw

Last update:
May 18, 2026

Description

A function equivalent of the  cfthrow  tag and is used in the <cfscript> mode.

Parameters

Same as the  <cfthrow>  tag.

Category

Function syntax

For name=value pair:
throw (message = "message", type = "exception type", detail, errorCode = "error code", extendedInfo = "additional info", object = "java exception object")
For positional notations, the sequence must be followed exactly in the same manner as provided in the syntax. If you do not provide one of the attributes, use an empty string instead.

History

ColdFusion (2025.0.08): Added support for reserved exception type values in throw() when the Java system property coldfusion.allow.restricted.exception.types is set to true. When enabled, throw() can use reserved ColdFusion exception types such as Database, MissingInclude, Template, Object, Security, Expression, and Lock.
ColdFusion 11: Parameters passed to this function need to be in a comma-separated format.
ColdFusion 9: Added this function.

See also

Usage

You can call this function by passing arguments as name=value pair or as positional arguments. For positional notations, specify the arguments in the sequence mentioned in the function syntax.
Use throw() in CFScript to generate an exception. In most applications, use a custom exception type or the default Application type for user-defined exceptions.
If the Java system property coldfusion.allow.restricted.exception.types is enabled, throw() can also use reserved ColdFusion exception types. This is useful when you need to preserve the original exception type while rethrowing or reconstructing an exception.

<cfscript>
    types = ["Database","MissingInclude","Template","Object","Security","Expression","Lock","Custom"];

    for (x in types) {
        try {
            throw(type=x, message="Test exception");
        } catch (any e) {
            writeOutput(e.type & ": " & e.message & "<br>");
        }
    }
</cfscript>

Example

<cfscript> 
function TotalInterest(principal, annualRate, months) 
{ var years = 0; 
var interestRate = 0; 
var totalInterest = 0; 
principal = REReplace(trim(principal), "[\$]", "", "ALL"); 
annualRate = Replace(trim(annualRate), "%", "", "ALL"); 

if ((principal <= 0) OR (annualRate <= 0) OR (months <= 0)) { 
Throw(type="InvalidData",message="All values must be greater than 0."); 

//Use of Throw function in cfscript 

interestRate = annualRate / 100; 
years = months / 12; 
totalInterest = principal * (((1 + interestRate) ^ years) - 1); 
return DollarFormat(totalInterest); 


try { 
WriteOutput(TotalInterest("$2500.00", "5.5%", "12")); 
} catch(InvalidData ex) { 
//Displayig exception details on screen 
WriteOutput("<p>An InvalidData exception was thrown.</p>"); 
WriteOutput("<p>#ex.message#</p>"); 

</cfscript>

Share this page

Was this page helpful?
We're glad. Tell us how this page helped.
We're sorry. Can you tell us what didn't work for you?
Thank you for your feedback. Your response will help improve this page.

On this page