Adobe COLDFUSION 9 Manual page 171

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

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Building Blocks of ColdFusion Applications
• Because ColdFusion passes structures, queries, and objects such as COM objects by reference, the argument name
exists only while the function executes, but the underlying data persists after the function returns and can be
accessed by using the variable name of the caller. The variable name of the caller and the argument name can be
different.
Note: If a function must use a variable from another scope that has the same name as a function-only variable, prefix the
external variable with its scope identifier, such as Variables or Form. (However, remember that using variables from
other scopes directly in your code is often poor practice.)
Handling errors in UDFs
ColdFusion provides several techniques to handle errors in UDFs:
• Display error messages directly in the function.
• Return function status information to the calling page.
• Use
/
or
/
try
catch
cftry
The technique you use depends on the circumstances of your function and application and on your preferred
programming style. However, it is best for most functions to use the second or third technique, or a combination of
the two.
Displaying error messages
Your function can test for errors and use the
This method is useful for providing immediate feedback to users for simple input errors. You can use it independently
or in conjunction with either of the other two error-handling methods.
For example, the following variation on a "Hello world" function displays an error message if you do not enter a name
in the form:
<cfform method="POST" action="#CGI.script_name#">
<p>Enter your Name:&nbsp;
<input name="name" type="text" hspace="30" maxlength="30">
<input type="Submit" name="submit" value="OK">
</cfform>
<cfscript>
function HelloFriend(Name) {
if (Name is "") WriteOutput("You forgot your name!");
else WriteOutput("Hello " & name &"!");
return "";
}
if (IsDefined("Form.submit")) HelloFriend(Form.name);
</cfscript>
Reviewing the code
The following table describes the code:
blocks and the
cfcatch
cfthrow
WriteOutput
Last updated 8/5/2010
and
tags to handle and generate exceptions.
cfrethrow
function to display an error message directly to the user.
166

Advertisement

Table of Contents
loading

Table of Contents