MACROMEDIA COLDFUSION MX 61-DEVELOPING COLDFUSION MX Develop Manual page 216

Developing coldfusion mx applications
Table of Contents

Advertisement

Code
<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
written using the
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.
Handling exceptions in UDFs
A UDF should use try/catch blocks to handle exceptions in the same conditions that any other
ColdFusion application uses try/catch blocks. These are typically circumstances where the
function uses an external resource, such as a Java, COM, or CORBA object, a database, or a file.
When possible, your application should prevent, rather than catch, exceptions caused by invalid
application data. For example, the application should prevent users from entering a zero value for
a form field that is used to divide another number, rather than handling exceptions generated by
dividing by zero.
When ColdFusion catches an exception, the function can use any of the following methods to
handle the exception:
If the error is recoverable (for example, if the problem is a database timeout where a retry might
resolve the issue), try to recover from the problem.
Display a message, as described in
Return an error status, as described in
If the UDF is defined using the
exception so that it will be caught by the calling ColdFusion page. For more information on
throwing and rethrowing exceptions, see
ColdFusion tags," on page
216
Chapter 10: Writing and Calling User-Defined Functions
tag can use the
cffunction
"Displaying error messages" on page
cffunction
299.
Description
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
,
,
cftry
cfcatch
"Providing status information" on page
tag, throw a custom exception, or rethrow the
Chapter 14, "Handling runtime exceptions with
and
statements. UDFs
catch
, and
cfthrow
cfrethrow
212.
214.
tags.

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion mx

Table of Contents