
169
DEVELOPING
COLDFUSION 9 APPLICATIONS
Building Blocks of ColdFusion Applications
Las
t
upda
te
d 8/5/2010
<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:
Using exceptions
UDFs written in CFScript can handle exceptions using the
try
and
catch
statements. UDFs written using the
cffunction
tag can use the
cftry
,
cfcatch
,
cfthrow
, and
cfrethrow
tags. Using exceptions corresponds to the way
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
Code
Description
<cfset status = StructNew()>
Creates a structure to hold the function status.
<cfset myInterest = TotalInterest
(Form.Principal, Form.AnnualPercent,
Form.Months, status)>
Calls the function. This time, the function requires four arguments,
including the status variable.
<cfif myInterest EQ -1>
<cfoutput>
ERROR: #status.errorMsg#<br>
</cfoutput>
If the function returns -1, there must be an error. Displays the message
that the function placed in the status.errorMsg structure key.
<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>
If the function does not return -1, it returns an interest value. Displays
the input values and the function return value.
Содержание COLDFUSION 9
Страница 1: ...Developing Applications ADOBE COLDFUSION 9...