Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cftry,cfcatch

Status
Not open for further replies.

zma

Programmer
May 30, 2006
25
US
I'm trying to learn error handling in ColdFusion and am having a problem not being able to catch a what the default error handler calls an "invalid CFML construct" with cfcatch. This example has a ] missing in the loop. I was hoping cfcatch with type="any" would get it. What am I doing wrong?

<html>
<head>
<title>
</title>
</head>
<body>

<!---
<cferror type="exception" template="qcfe.cfm">
--->

<cftry>
<cfquery name="qerr" datasource="#dso#">
select code,descriptor
from tblnewtax
order by code
</cfquery>
<cfset upto=qerr.recordcount>
<cfif upto gt 0>
<cfoutput>
<cfloop index="ii" from="1" to="#upto#">
#ii#,#qerr.code[ii]#,#qerr.descriptor[ii#
<br>
</cfloop>
</cfoutput>
</cfif>
<cfcatch type="database">
<cfset errdbf=true>
dbf error
<cfabort>
</cfcatch>
<cfcatch type="any">
other error
<cfabort>
</cfcatch>
</cftry>
</body>
</html>
 
As far as I know, you can only have one <cfcatch> call in a <cftry>, from your example you're calling two <cfcatch>'s but only one <cftry>

Are you getting anything back at all?

Take a look at:
Code:
<html>
	<head><title></title></head>
	<body>
		<!---cferror type="exception" template="qcfe.cfm"--->
		<cftry>
			<cfquery name="qerr" datasource="#dso#">
				select code,descriptor
				from tblnewtax
				order by code
			</cfquery>
			<cfset upto=qerr.recordcount>

			<cfif upto gt 0>
				<cfoutput>
					<cfloop index="ii" from="1" to="#upto#">
						#ii#,#qerr.code[ii]#,#qerr.descriptor[ii#
						<br>
					</cfloop>
				</cfoutput>
			</cfif>
		<cfcatch type="database">
			<cfset errdbf=true>
			dbf error
			
			[COLOR=red]
			<!--- MAIL YOURSELF THE CFDUMP OF ERROR --->
			<cfmail to="" from="" subject="CFCATCH error">
				<cfdump var="#cfcatch#" label="cfcatch error">
			</cfmail>
			
			<!--- REDIRECT USER SO THEY GET A CUSTOM FRIENDLY MESSAGE --->
			<cflocation url="errorpage.cfm">
			[/color]
		</cfcatch>
		</cftry>
	</body>
</html>

The portion in red dictates a better practice on error code handeling.

____________________________________
Just Imagine.
 
Thankyou for the code in red and yes I am getting something back.
(1) If I make a spelling error in the query, like selec instead of select or code0 instead of code, cfcatch shows "dbf error."
(2) If there is a misspelling in the loop, like err.code instead of qerr.code, cfcatch shows "other error."
(3) If there errors in both, only the 1st cfcatch is done whether or not the cfabort is there.
((4)) If there is an error like a quote mark left off around the data source in the query, I get a CF default error about "invalid token" and if there is an error like leaving a bracket out of the display part of the loop, I get a CF default error "invalid CMFL construct."
How can I catch those errors myself?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top