Phillip Senn
Phillip Senn

Reputation: 47605

Best practice for cftry/cfcatch

In ColdFusion 8 or below, is the marked line in the right place?

<cftry>
  <cfquery name="local.qry" datasource="myDatasource">
    SELECT ID FROM TableName
    WHERE ...
  </cfquery>
  <cfset local.result = local.qry.ID><!--- this line! --->
  <cfcatch>
    <cfset local.result = Variables.objDatabase.CatchError(cfcatch)>
  </cfcatch>
</cftry>

<cfreturn local.result>

Upvotes: 2

Views: 2584

Answers (1)

Shawn Grigson
Shawn Grigson

Reputation: 927

Yes. You could set it outside of the block, but why? At least this way, you'll catch any bizarre errors that might occur during assignment. It's not likely, but the query could succeed and the assignment could fail, so why not trap that potential issue?

You've already got the overhead of a try/catch, might as well add the assignment to the try block as well.

Upvotes: 5

Related Questions