CFCATCH Tag - ColdFusion


>> Return to CF Tag List

The CFCATCH tag is used inside the CFTRY tag. You use it to include customized error handling on your web page. If an error happens on ColdFusion code that resides inside your CFTRY tags, ColdFusion will cause the code inside your CFCATCH tags to start executing.

Syntax:
  <CFCATCH [TYPE="Exception Type"]>


Attributes:
ATTRIBUTE DEFAULT VALUE POSSIBLE VALUES COMMENTS
TYPE Any "Application" - catch application errors
"Any" - catch any error
"Database" - catch database errors
Optional

CFCATCH variables
Note that there is also a CFCATCH variable structure. When an error happens, you can check this variable to return different information about the error that happened. You can display this information perhaps. Or, more likely, you can use it with conditional statements to determine what your code is going to do next.

CFCATCH Variable Variable Content
CFCATCH.TYPE This is the exception type, as specified in cfcatch.
CFCATCH.MESSAGE A diagnostic message if available. If it's not available, this variable will be blank. This is like the human-readable version of the error.
CFCATCH.DETAIL This is the detailed version of the error message. This message can contain formatting HTML. When debugging, it is nice to output this to the browser to find out more information about the error. You can use this information to help determine which ColdFusion tag was responsible for the error.



  CFCATCH Comments from Adobe LiveDocsLiveDocs Comments - coldfusion - 6.1 - htmldocs - tags-a10.htm
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 !

LiveDocs Comments - coldfusion - 6.1 - htmldocs - tags-a10.htm
It would be nice if one were able to do

<cfdump var="#cfcatch#">

LiveDocs Comments - coldfusion - 6.1 - htmldocs - tags-a10.htm
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.

LiveDocs Comments - coldfusion - 6.1 - htmldocs - tags-a10.htm
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.

LiveDocs Comments - coldfusion - 6.1 - htmldocs - tags-a10.htm
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>

LiveDocs Comments - coldfusion - 6.1 - htmldocs - tags-a10.htm
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).

LiveDocs Comments - coldfusion - 6.1 - htmldocs - tags-a10.htm
How about one for LDAP.

LiveDocs Comments - coldfusion - 6.1 - htmldocs - tags-a10.htm
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

LiveDocs Comments - coldfusion - 6.1 - htmldocs - tags-a10.htm
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?

LiveDocs Comments - coldfusion - 6.1 - htmldocs - tags-a10.htm
<cfdump var="#cfcatch.tagcontext#"> is simpler than your loop example.

LiveDocs Comments - coldfusion - 6.1 - htmldocs - tags-a10.htm
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#

Error Detail: #cfcatch.Detail#

Line: #sCurrent["LINE"]#

Column: #sCurrent["COLUMN"]#

Template: #sCurrent["TEMPLATE"]#



</cfloop>
</cfoutput>
</cfcatch>
</cftry>

LiveDocs Comments - coldfusion - 6.1 - htmldocs - tags-a10.htm
To pass the CFCATCH variable to a UDF expecting a structure you have to duplicate() it.

http://www.rewindlife.com/archives/000135.cfm

LiveDocs Comments - coldfusion - 6.1 - htmldocs - tags-a10.htm
Is there a support to capture faultCode for web services in cfcatch ? That would be very much useful
-Narayan



[Top]


Recommended Books

[Top]

Web Sites

[Top]

Copyright © 2003- 2010, Orville Paul Chomer, All Rights Reserved