I have the following try/catch block, but still ge...
# cfml-beginners
e
I have the following try/catch block, but still get this error: Element QUERYERROR is undefined in CFCATCH. What am I not doing to catch it?
Copy code
<CFTRY>
	<cfstoredproc procedure="UPDATE_PKG.MAIN_P" datasource="#REQUEST.prod#">
		<cfprocparam type="In" value=#FORM.sales_order_number# cfsqltype="CF_SQL_INTEGER">
		<cfprocparam type="In" value=#FORM.division_id# cfsqltype="CF_SQL_VARCHAR">
	</cfstoredproc>
	
	<cfoutput>
		<h3 align="left">Success</h3>
		<p></p><a href="#CGI.http_referer#">Go Back</a>
	</cfoutput>	

	<CFCATCH TYPE = "Database">
		<cfoutput>   
			Something went wrong :-( Contact your system administrator<br />
			#cfcatch.queryError#			
		</cfoutput>
 	</CFCATCH>
</CFTRY>
m
Try dumping your
cfcatch
to see what keys are available.
Also, this idea may be a little out there, but perhaps the
type
attribute is case-sensitive?
a
Yeah, not all keys in
cfcatch
always exist. So your code is erroring when it runs, but then inside the error handling (the
cfcatch
block) you are referencing something that doesn't exist.
Also - I wouldn't output the error information as you are exposing information about your applications / database which hackers could use to exploit it. You should log the error and put out a generic "sorry" message to the end user.
e
Thanks for replies. It looks like TYPE needs to be "database" and dump revealed no queryError in it. I guess the error happened before sql was passed into a database. Should I use TYPE="any"? Will it work with database errors?
m
If you were planning to handle errors differently, I might use the type attribute, but yours appears to be generic ("something went wrong"), so I would use "any" (which I believe is the default anyway).
👆 2
e
I think I just answered my own question by entering 1/0 into my stored proc. Type="any" returned "Error: Error Executing Database Query.", but "database" returned much more useful data: "ORA-01476: divisor is equal to zero"
So, how do I combine the two? I need to catch errors with type="any" before sql is passed into a database, but would like to have detailed error msg with type="database" when proc is running.
a
You can have multiple catch types. For example:
Copy code
<cftry>
  <!--- run some code --->
  <cfcatch type="database">
    <!--- handle a database error --->
  </cfcatch>
  <cfcatch type="any">
    <!--- handle any other type of error --->
  </cfcatch>
</cftry>
👍 1
👍🏼 1
e
Thank you both!