User Guide

Table Of Contents
CFScript example 137
Exception handling example
The following code shows exception handling in CFScript. It uses a
CreateObject function to
create a Java object. The catch statement executes only if the
CreateObject function generates an
exception. The displayed information includes the exception message; the except.Message variable
is the equivalent of calling the Java
getMessage method on the returned Java exception object.
<cfscript>
try {
emp = CreateObject("Java", "Employees");
}
catch(Any excpt) {
WriteOutput("The application was unable to perform a required
operation.<br>
Please try again later.<br>If this problem persists, contact
Customer Service and include the following information:<br>
#excpt.Message#<br>");
}
</cfscript>
CFScript example
The example in this section uses the following CFScript features:
Variable assignment
Function calls
For loops
If-else statements
WriteOutput functions
Switch statements
The example uses CFScript without any other ColdFusion tags. It creates a structure of course
applicants. This structure contains two arrays; the first has accepted students, the second has
rejected students. The script also creates a structure with rejection reasons for some (but not all)
rejected students. It then displays the accepted applicants followed by the rejected students and
their rejection reasons.
<html>
<head>
<title>CFScript Example</title>
</head>
<body>
<cfscript>
//Set the variables
acceptedApplicants[1] = "Cora Cardozo";
acceptedApplicants[2] = "Betty Bethone";
acceptedApplicants[3] = "Albert Albertson";
rejectedApplicants[1] = "Erma Erp";
rejectedApplicants[2] = "David Dalhousie";
rejectedApplicants[3] = "Franny Farkle";