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, onError; Chapter 14, "Handling Errors" in ColdFusion MX Developer’s Guide

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 = "cfdocexamples">
		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>
		</cfoutput>
	</cfcatch>
</cftry>