View comments | RSS feed
Contents > CFML Reference > ColdFusion Tags > cfcatch PreviousNext

cfcatch

Used inside a cftry tag. 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.

Exception handling tags

<cfcatch type = "exceptiontype">
Exception processing code here
</cfcatch>

cftry, cferror, cfrethrow, cfthrow, Handling Errors in Developing ColdFusion MX Applications

ColdFusion MX:

Attribute

Req/Opt

Default

Description

type

Optional

Any

  • application: catches application exceptions
  • database: catches database exceptions
  • template: catches ColdFusion page exceptions
  • security: catches security exceptions
  • object: catches object exceptions
  • missingInclude: catches missing include file exceptions
  • expression: catches expression exceptions
  • lock: catches lock exceptions
  • custom_type: catches the specified custom exception type that is defined in a cfthrow tag
  • searchengine: catches Verity search engine exceptions
  • any: catches all exception types

You must code at least one cfcatch tag within a cftry block. Put cfcatch tags at the end of a cftry block. ColdFusion MX tests cfcatch tags in the order in which they appear. This tag requires an end tag.

If type="any", ColdFusion MX catches exceptions from any CFML tag, data source, or external object. To get the exception type use code such as the following:

   #cfcatch.type#

Applications can use the cfthrow tag to throw developer-defined exceptions. Catch these exceptions with any of these type options:

The custom_type type is a developer-defined type specified in a cfthrow tag. If you define a custom type as a series of strings concatenated by periods (for example, "MyApp.BusinessRuleException.InvalidAccount"), ColdFusion MX can catch the custom type by its character pattern. ColdFusion MX searches for a cfcatch tag in the cftry block with a matching exception type, starting with the most specific (the entire string), and ending with the least specific.

For example, you could define a type as follows:

<cfthrow type = "MyApp.BusinessRuleException.InvalidAccount">

If you have the following cfcatch tag, it will handle the exception:

<cfcatch type = "MyApp.BusinessRuleException.InvalidAccount"> 

Otherwise, if you have the following cfcatch tag, it will handle the exception:

<cfcatch type = "MyApp.BusinessRuleException"> 

Finally, if you have the following cfcatch tag, it will handle the exception:

<cfcatch type = "MyApp"> 

You can code cfcatch tags in any order to catch a custom exception type.

If you specify type = "Application", the cfcatch tag catches only custom exceptions that have the Application type in the cfthrow tag that defines them.

The cfinclude, cfmodule, and cferror tags throw an exception of type = "template".

An exception that is thrown within a cfcatch block cannot be handled by the cftry block that immediately encloses the cfcatch tag. However, you can rethrow the currently active exception with the cfrethrow tag.

The cfcatch variables provide the following exception information:

cfcatch variable 

Content

cfcatch.type

Type: Exception type, as specified in cfcatch.

cfcatch.message 

Message: Exception's diagnostic message, if provided; otherwise, an empty string; in the cfcatch.message variable

cfcatch.detail 

Detailed message from the CFML interpreter or specified in a cfthrow tag. When the exception is generated by ColdFusion (and not cfthrow), the message can contain HTML formatting and can help determine which tag threw the exception.

cfcatch.tagcontext 

An array of tag context structures, each representing one level of the active tag context at the time of the exception.

cfcatch.NativeErrorCode

Applies to type = "database". Native error code associated with exception. Database drivers typically provide error codes to diagnose failing database operations. Default: -1.

cfcatch.SQLState

Applies to type ="database". SQLState associated with exception. Database drivers typically provide error codes to help diagnose failing database operations. Default: -1.

cfcatch.Sql

Applies to type ="database". The SQL statement sent to the data source.

cfcatch.queryError

Applies to type ="database". The error message as reported by the database driver.

cfcatch.where

Applies to type ="database". If the query uses the cfqueryparam tag, query parameter name-value pairs.

cfcatch.ErrNumber

Applies to type="expression". Internal expression error number.

cfcatch.MissingFileName 

Applies to type="missingInclude". Name of file that could not be included.

cfcatch.LockName

Applies to type="lock". Name of affected lock (if the lock is unnamed, the value is "anonymous").

cfcatch.LockOperation

Applies to type="lock". Operation that failed (Timeout, Create Mutex, or Unknown).

cfcatch.ErrorCode

Applies to type="custom". String error code.

cfcatch.ExtendedInfo

Applies to type="application" and "custom". Custom error message; information that the default exception handler does not display.

Advanced Exception types

You can specify the following advanced exception types in the type attribute:

ColdFusion advanced exception type

COM.Allaire.ColdFusion.CFEXECUTE.OutputError

COM.Allaire.ColdFusion.CFEXECUTE.Timeout

COM.Allaire.ColdFusion.FileException

COM.Allaire.ColdFusion.HTTPAccepted

COM.Allaire.ColdFusion.HTTPAuthFailure

COM.Allaire.ColdFusion.HTTPBadGateway

COM.Allaire.ColdFusion.HTTPBadRequest

COM.Allaire.ColdFusion.HTTPCFHTTPRequestEntityTooLarge

COM.Allaire.ColdFusion.HTTPCGIValueNotPassed

COM.Allaire.ColdFusion.HTTPConflict

COM.Allaire.ColdFusion.HTTPContentLengthRequired

COM.Allaire.ColdFusion.HTTPContinue

COM.Allaire.ColdFusion.HTTPCookieValueNotPassed

COM.Allaire.ColdFusion.HTTPCreated

COM.Allaire.ColdFusion.HTTPFailure

COM.Allaire.ColdFusion.HTTPFileInvalidPath

COM.Allaire.ColdFusion.HTTPFileNotFound

COM.Allaire.ColdFusion.HTTPFileNotPassed

COM.Allaire.ColdFusion.HTTPFileNotRenderable

COM.Allaire.ColdFusion.HTTPForbidden

COM.Allaire.ColdFusion.HTTPGatewayTimeout

COM.Allaire.ColdFusion.HTTPGone

COM.Allaire.ColdFusion.HTTPMethodNotAllowed

COM.Allaire.ColdFusion.HTTPMovedPermanently

COM.Allaire.ColdFusion.HTTPMovedTemporarily

COM.Allaire.ColdFusion.HTTPMultipleChoices

COM.Allaire.ColdFusion.HTTPNoContent

COM.Allaire.ColdFusion.HTTPNonAuthoritativeInfo

COM.Allaire.ColdFusion.HTTPNotAcceptable

COM.Allaire.ColdFusion.HTTPNotFound

COM.Allaire.ColdFusion.HTTPNotImplemented

COM.Allaire.ColdFusion.HTTPNotModified

COM.Allaire.ColdFusion.HTTPPartialContent

COM.Allaire.ColdFusion.HTTPPaymentRequired

COM.Allaire.ColdFusion.HTTPPreconditionFailed

COM.Allaire.ColdFusion.HTTPProxyAuthenticationRequired

COM.Allaire.ColdFusion.HTTPRequestURITooLarge

COM.Allaire.ColdFusion.HTTPResetContent

COM.Allaire.ColdFusion.HTTPSeeOther

COM.Allaire.ColdFusion.HTTPServerError

COM.Allaire.ColdFusion.HTTPServiceUnavailable

COM.Allaire.ColdFusion.HTTPSwitchingProtocols

COM.Allaire.ColdFusion.HTTPUnsupportedMediaType

COM.Allaire.ColdFusion.HTTPUrlValueNotPassed

COM.Allaire.ColdFusion.HTTPUseProxy

COM.Allaire.ColdFusion.HTTPVersionNotSupported

COM.Allaire.ColdFusion.POPAuthFailure

COM.Allaire.ColdFusion.POPConnectionFailure

COM.Allaire.ColdFusion.POPDeleteError

COM.Allaire.ColdFusion.Request.Timeout

COM.Allaire.ColdFusion.SERVLETJRunError

COMCOM.Allaire.ColdFusion.HTTPConnectionTimeout

<!--- 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>
      </cfoutput>
   </cfcatch>
</cftry>

Contents > CFML Reference > ColdFusion Tags > cfcatch PreviousNext

ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


No screen name said on Aug 21, 2003 at 5:14 PM :
Is there a support to capture faultCode for web services in cfcatch ? That would be very much useful
-Narayan
_Sam_ said on Feb 7, 2004 at 11:49 AM :
To pass the CFCATCH variable to a UDF expecting a structure you have to duplicate() it.

http://www.rewindlife.com/archives/000135.cfm
wlee said on Mar 22, 2004 at 7:33 AM :
We are working to improve the examples in the ColdFusion reference pages. We propose to replace the current example on this page with the the following example. If you have any comments on this example, add them to this page.

<!--- 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]#>
#sCurrent["ID"]# #cfcatch.Message#<br>
Error Detail: #cfcatch.Detail#<br>
Line: #sCurrent["LINE"]#<br>
Column: #sCurrent["COLUMN"]#<br>
Template: #sCurrent["TEMPLATE"]#



</cfloop>
</cfoutput>
</cfcatch>
</cftry>
omniphiliac said on Apr 16, 2004 at 2:51 PM :
<cfdump var="#cfcatch.tagcontext#"> is simpler than your loop example.
webdev101 said on Jul 12, 2004 at 9:34 AM :
Why am I unable to pass cfcatch as a type struct into a function. I am having to copy the values into a new struct and pass that in. Why won't cffunction accept cfcatch as a struct then? It won't accept newVar if newVar=cfcatch. I have to get them out one by one ie; newVar.message = cfcatch.message, etc. then I can pass newVar to the function as a type struct. Am I missing something?
G0V. said on Oct 21, 2004 at 5:17 AM :
No screen name said on Aug 21, 2003 at 5:14 PM :
Is there a support to capture faultCode for web services in cfcatch ? That would be very much useful
-Narayan

Same question here !
I'm trying to catch possible errors on the Google Webservice using cftry/cfcatch but doesn't work, there should be a type="structure" or something
N8iveTXn said on Oct 27, 2004 at 7:47 AM :
How about one for LDAP.
dlakein said on Oct 29, 2004 at 11:41 AM :
We have a cfcatch handler (also exception handler) that loops over the tagcontext and builds a similar string.

I just found that ID ( cfcatch.tagContext[i].ID) may not exist (like in syntax errors), and trying to output it will generate a java error (hashtable, IIRC).
myhale said on Dec 22, 2004 at 2:04 PM :
You can use this code snippet to check for the ID key.
<cfloop index = i from = 1 to = #ArrayLen(cfcatch.tagContext)#>
<cfset sCurrent = #cfcatch.tagContext[i]#>
<BR>#i# <cfif StructKeyExists(sCurrent, "ID")> #sCurrent["ID"]#</cfif>(#sCurrent["LINE"]#,#sCurrent["COLUMN"]#)#sCurrent["TEMPLATE"]#
</cfloop>
a440guy said on Jan 3, 2005 at 4:32 PM :
Missing information:

Right after the first paragraph (in the section headed "cfcatch"), you need to say that, when an exception occurs, a structure called "cfcatch" is produced (or "returned", or even better, "springs into existence") and that said structure contains data related to the exception. (You might also mention that, with nested <cftry> blocks, the cfcatch structures at each level are in separate namespaces, thus there will be no name collisions (I don't know if that's true, because you don't say, but it is absolutly essential to know). You should immediately follow that with a high-level description of the structure (e.g., "Most elements of the structure contain scalar values, but some elements contain structures or arrays"). You should then refer readers to the section below that describes the various elements of the structure.

Ambiguous:

In the section headed "ColdFusion MX:", you say "Changed object modification: you cannot modify the object returned by cfcatch." This statement refers to something that you haven't mentioned yet. It can be solved by doing what I suggest above, and adding to that the use of the term "object".

Ambiguous:

Just above the HTML table with headings "catch variable" and "content", you say, "The cfcatch variables provide the following exception information". What cfcatch variables? You have only implied their existence, you haven't described them yet. By the way, should that have been written in plural number? Are there several cfcatch variables or is there a single variable (object) with several fields? (If you want to use the plural, then call them by their more-informative names: instance variables)

Incomplete:

The table mentioned above does not indicate what type of data the "variable" (or field) contains. Please indicate whether it is scalar, array, or structure. Although we can deduce, lacking any mention otherwise, that the data is scalar, it is good form for you to say that explicitly. The one field where you indicate that it is an array, you don't go far enough. You say that it is an array of structures which you don't describe. We're back where we started. Do you expect us to write some code to enumerate the fieldnames of this structure? And, if you are tempted to reply, "we show the fieldnames in the example", let me explain something: an example is NOT the place to explain or present new information. It is supposed to be an EXAMPLE of the information you presented. By looking at the example, we can see that there are at least three fields in this structure, "line", "column", and "template". But we don't know if there are any others. Is this a rigorous example? We don't know. Even if the use of all the fields is demonstrated in the example, it is very difficult (not to mention annoying) to have to read the so-called example to find this information. This is poor practice.
mindtrap said on Jan 6, 2005 at 6:51 AM :
Since the latest CFMX patch which I believe is dated August 25,2005 you no longer are able to do a CFDump of the CFCATCH object. I've filed a bug report, but have yet to hear anything of it.
No screen name said on Jun 6, 2005 at 8:26 PM :
It would be nice if one were able to do

<cfdump var="#cfcatch#">
Murrah said on Sep 9, 2005 at 2:18 AM :
It seems that if there is no gap between the CFCatch tags, the cffile tag is ignored:

<cftry>
<cffile action = "delete" file = "#somefilename#">
<cfcatch></cfcatch>
</cftry>

but if there is a space between <cfcatch> </cfcatch>, the file deletion DOES work !

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/tags-a10.htm