Adobe COLDFUSION 9 Manual page 174

Developing applications
Hide thumbs Also See for COLDFUSION 9:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Building Blocks of ColdFusion Applications
<cfset status = StructNew()>
<cfset myInterest = TotalInterest(Form.Principal,
Form.AnnualPercent,Form.Months, status)>
<cfif myInterest EQ -1>
<cfoutput>
ERROR: #status.errorMsg#<br>
</cfoutput>
<cfelse>
<cfoutput>
Loan amount: #Form.Principal#<br>
Annual percentage rate:
#Form.AnnualPercent#<br>
Loan duration: #Form.Months# months<br>
TOTAL INTEREST: #myInterest#<br>
</cfoutput>
</cfif>
Reviewing the code
The following table describes the code that has been changed or added:
Code
<cfset status = StructNew()>
<cfset myInterest = TotalInterest
(Form.Principal, Form.AnnualPercent,
Form.Months, status)>
<cfif myInterest EQ -1>
<cfoutput>
ERROR: #status.errorMsg#<br>
</cfoutput>
<cfelse>
<cfoutput>
Loan amount: #Form.Principal#<br>
Annual percentage rate:
#Form.AnnualPercent#<br>
Loan duration: #Form.Months# months<br>
TOTAL INTEREST: #myInterst#<br>
</cfoutput>
</cfif>
Using exceptions
UDFs written in CFScript can handle exceptions using the
tag can use the
cffunction
cftry
many functions in other programming languages handle errors, and can be an effective way to handle errors. In
particular, it separates the functional code from the error-handling code, and it can be more efficient than other
methods at runtime, because it does not require testing and branching.
Exceptions in UDFs have the following two dimensions:
• Handling exceptions generated by running the UDF code
• Generating exceptions when the UDF identifies invalid data or other conditions that would cause errors if
processing continued
Description
Creates a structure to hold the function status.
Calls the function. This time, the function requires four arguments,
including the status variable.
If the function returns -1, there must be an error. Displays the message
that the function placed in the status.errorMsg structure key.
If the function does not return -1, it returns an interest value. Displays
the input values and the function return value.
try
,
,
, and
cfcatch
cfthrow
cfrethrow
Last updated 8/5/2010
and
statements. UDFs written using the
catch
tags. Using exceptions corresponds to the way
169

Advertisement

Table of Contents
loading

Table of Contents