|
ColdFusion 9.0 Resources |
cfthrowDescriptionThrows a developer-specified exception, which can be caught with a cfcatch tag that has any of the following type attribute options:
Syntax<cfthrow
detail = "detail description"
errorCode = "error code"
extendedInfo = "additional information"
message = "message"
object = "java except object"
type = "exception type">
OR
<cfthrow
object = #object_name#>
Note: You
can specify this tag’s attributes in an attributeCollection attribute
whose value is a structure. Specify the structure name in the attributeCollection attribute
and use the tag’s attribute names as structure keys.
See alsocferror, cfrethrow, cftry, onError; Handling Errors in the Developing ColdFusion Applications HistoryColdFusion MX: Changed thrown exceptions: this tag can throw ColdFusion component method exceptions. Attributes
UsageUse this tag within a cftry block, to throw an error. The cfcatch block can access accompanying information, as follows:
Example<h3>cfthrow Example</h3>
<!--- Open a cftry block. --->
<cftry>
<!--- Define a condition upon which to throw the error. --->
<cfif NOT IsDefined("URL.myID")>
<!--- throw the error --->
<cfthrow message = "ID is not defined">
</cfif>
<!--- Perform the error catch. --->
<cfcatch type = "application">
<!--- Display your message. --->
<h3>You've Thrown an <b>Error</b></h3>
<cfoutput>
<!--- And the diagnostic feedback from the application server. --->
<p>#cfcatch.message#</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>
The following example shows how to throw an exception from a component method: <cfcomponent>
<cffunction name="getEmp">
<cfargument name="lastName" required="yes">
<cfquery name="empQuery" datasource="cfdocexamples" >
SELECT LASTNAME, FIRSTNAME, EMAIL
FROM tblEmployees
WHERE LASTNAME LIKE '#arguments.lastName#'
</cfquery>
<cfif empQuery.recordcount LT 1>
<cfthrow type="noQueryResult"
message="No results were found. Please try again.">
<cfelse>
<cfreturn empQuery>
</cfif>
</cffunction>
</cfcomponent>
For an explanation of the example and more information, see Building and Using ColdFusion Components in the Developing ColdFusion Applications. |