User Guide

Table Of Contents
330 Chapter 14: Handling Errors
Reviewing the code
The following table describes the code:
Code Description
<cfset EmpID=3>
<cfparam name="errorCaught" default="">
Initializes the employee ID to a valid value. An
application would get the value from a form or
other source.
Sets the default
errorCaught variable value to
the empty string (to indicate no error was
caught).
There is no need to put these lines in a
cftry
block.
<cftry>
<cfquery name="test"
datasource="cfdocexamples">
SELECT Dept_ID, FirstName, LastName
FROM Employee
WHERE Emp_ID=#EmpID#
</cfquery>
Starts the cftry block. Exceptions from here to
the end of the block can be caught by
cfcatch
tags.
Queries the cfdocexamples database to get the
data for the employee identified by the
EmpID
variable.
<html>
<head>
<title>Test cftry/cfcatch</title>
</head>
<body>
<cfinclude template="includeme.cfm">
<cfoutput query="test">
<p>Department: #Dept_ID#<br>
Last Name: #LastName#<br>
First Name: #FirstName#</p>
</cfoutput>
Begins the HTML page. This section contains all
the code that displays information if no errors
occur.
Includes the includeme.cfm page.
Displays the user information record from the
test query.
<cfcatch type="MissingInclude">
<h1>Missing Include File</h1>
<cfoutput>
<ul>
<li><b>Message:</b> #cfcatch.Message#
<li><b>Detail:</b> #cfcatch.Detail#
<li><b>File name:</b>
#cfcatch.MissingFilename#
</ul>
</cfoutput>
<cfset errorCaught = "MissingInclude">
</cfcatch>
Handles exceptions thrown when a page
specified by the
cfinclude tag cannot be found.
Displays
cfcatch variables, including the
ColdFusion basic error message, detail
message, and the name of the file that could not
be found.
Sets the
errorCaught variable to indicate the
error type.
<cfcatch type="Database">
<h1>Database Error</h1>
<cfoutput>
<ul>
<li><b>Message:</b> #cfcatch.Message#
<li><b>Native error code:</b>
#cfcatch.NativeErrorCode#
<li><b>SQLState:</b> #cfcatch.SQLState#
<li><b>Detail:</b> #cfcatch.Detail#
</ul>
</cfoutput>
<cfset errorCaught = "Database">
</cfcatch>
Handles exceptions thrown when accessing a
database.
Displays
cfcatch variables, including the
ColdFusion basic error message, the error code
and SQL state reported by the databases
system, and the detailed error message.
Sets the
errorCaught variable to indicate the
error type.