User Guide

Table Of Contents
Handling errors in UDFs 187
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:
Providing status information
In some cases, such as those where the function cannot provide a corrective action, the function
cannot, or should not, handle the error directly. In these cases, your function can return
information to the calling page. The calling page must handle the error information and act
appropriately.
Code Description
<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>
Creates a simple form requesting you to enter your
name.
Uses the script_name CGI variable to post to this
page without specifying a URL.
If you do not enter a name, the form posts an
empty string as the name field.
<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>
Defines a function to display "Hello name!" First,
checks whether the argument is an empty string. If
so, displays an error message.
Otherwise displays the hello message.
Returns the empty string. (The caller does not use
the return value). It is not necessary to use curly
braces around the if or else statement bodies
because they are single statements.
If this page has been called by submitting the form,
calls the HelloFriend function. Otherwise, the page
just displays the form.