216
Chapter 10: Writing and Calling User-Defined Functions
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.
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
“Displaying error messages” on page 212
.
•
Return an error status, as described in
“Providing status information” on page 214
.
•
If the UDF is defined using the
cffunction
tag, throw a custom exception, or rethrow the
exception so that it will be caught by the calling ColdFusion page. For more information on
throwing and rethrowing exceptions, see
Chapter 14, “Handling runtime exceptions with
ColdFusion tags,” on page 299
.
<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.
Code
Description
Summary of Contents for COLDFUSION MX 61-DEVELOPING COLDFUSION MX
Page 1: ...Developing ColdFusion MX Applications...
Page 22: ...22 Contents...
Page 38: ......
Page 52: ...52 Chapter 2 Elements of CFML...
Page 162: ......
Page 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Page 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Page 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Page 266: ......
Page 314: ...314 Chapter 14 Handling Errors...
Page 344: ...344 Chapter 15 Using Persistent Data and Locking...
Page 349: ...About user security 349...
Page 357: ...Security scenarios 357...
Page 370: ...370 Chapter 16 Securing Applications...
Page 388: ...388 Chapter 17 Developing Globalized Applications...
Page 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Page 410: ......
Page 426: ...426 Chapter 19 Introduction to Databases and SQL...
Page 476: ...476 Chapter 22 Using Query of Queries...
Page 534: ...534 Chapter 24 Building a Search Interface...
Page 556: ...556 Chapter 25 Using Verity Search Expressions...
Page 558: ......
Page 582: ...582 Chapter 26 Retrieving and Formatting Data...
Page 668: ......
Page 734: ...734 Chapter 32 Using Web Services...
Page 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Page 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Page 788: ......