User Guide

cftry 353
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
Exception handling tags
Syntax
<cftry>
Code that might throw an exception
One or more cfcatch blocks
</cftry>
See also
cfcatch, cferror, cfrethrow, cfthrow,
Chapter 14, “Handling Errors,” in Developing
ColdFusion MX 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
<!--- cftry example, using TagContext to display the tag stack. --->
<h3>cftry Example</h3>
<!--- open a cftry block --->
<cftry>
<!--- note misspelled tablename "employees" as "employeeas" --->
<cfquery name = "TestQuery" dataSource = "cfsnippets">
SELECT *
FROM EMPLOYEEAS
</cfquery>
<!--- <p>... other processing goes here --->
<!--- specify the type of error for which we search --->
<cfcatch type = "Database">
<!--- the message to display --->
<h3>You've Thrown a Database <b>Error</b></h3>
<cfoutput>
<!--- and the diagnostic message from the ColdFusion server --->
<p>#cfcatch.message#</p>
<p>Caught an exception, type = #CFCATCH.TYPE# </p>
<p>The contents of the tag stack are:</p>
<cfloop index = i from = 1
to = #ArrayLen(CFCATCH.TAGCONTEXT)#>
<cfset sCurrent = #CFCATCH.TAGCONTEXT[i]#>
<br>#i# #sCurrent["ID"]#
(#sCurrent["LINE"]#,#sCurrent["COLUMN"]#)
#sCurrent["TEMPLATE"]#
</cfloop>