cftry

 

Description

Used with one or more cfcatch tags. Together, they catch and process exceptions in ColdFusion pages. Exceptions are events that disrupt the normal flow of instructions in a ColdFusion page, such as failed database operations, missing include files, and developer-specified events.

Category

Syntax

Code that might throw an exception
One or more cfcatch blocks
</cftry>
Code that might throw an exception One or more cfcatch blocks </cftry>
Code that might throw an exception
One or more cfcatch blocks
</cftry>

See also

cfcatchcffinallycferrorcfrethrowcfthrowonErrorHandling Errors in the Developing ColdFusion Applications

History

ColdFusion MX: Changed cfscript to include try and catch statements that are equivalent to the cftry and cfcatch tags.

Usage

Within a cftry block, put the code that might throw an exception, followed by one ore more cfcatch tags that catch and process exceptions. This tag requires an end tag.

Example

 

<cfscript>
myQuery = queryNew("id,name,amount","Integer,Varchar,Integer",
[
{id=1,name="One",amount=15},
{id=2,name="Two",amount=18},
{id=3,name="Three",amount=32}
]);
</cfscript>
<cftry>
<cfloop query = "myQuery">
<!--- Use a non-existent column --->
<cfoutput>#location#</cfoutput>
</cfloop>
<cfcatch type="any">
<p><strong>Incorrect column referred. Please use the correct column.<strong><p>
</cfcatch>
</cftry>
<cfscript> myQuery = queryNew("id,name,amount","Integer,Varchar,Integer", [ {id=1,name="One",amount=15}, {id=2,name="Two",amount=18}, {id=3,name="Three",amount=32} ]); </cfscript> <cftry> <cfloop query = "myQuery"> <!--- Use a non-existent column ---> <cfoutput>#location#</cfoutput> </cfloop> <cfcatch type="any"> <p><strong>Incorrect column referred. Please use the correct column.<strong><p> </cfcatch> </cftry>
<cfscript>
    myQuery = queryNew("id,name,amount","Integer,Varchar,Integer", 
                [ 
                        {id=1,name="One",amount=15}, 
                        {id=2,name="Two",amount=18}, 
                        {id=3,name="Three",amount=32} 
                ]); 
</cfscript>
<cftry>
    <cfloop query = "myQuery">
        <!--- Use a non-existent column --->
        <cfoutput>#location#</cfoutput> 
    </cfloop>
    <cfcatch type="any">
            <p><strong>Incorrect column referred. Please use the correct column.<strong><p>
    </cfcatch>
</cftry>

Output

Incorrect column referred. Please use the correct column.

Get help faster and easier

New user?