BackUp LevelNext

Exception Handling Example

The following example shows CFTRY and CFCATCH, using a sample data source called company and a sample included file, includeme.cfm.

If the data access driver raises an exception during the CFQUERY statement's execution, the application page flow continues to the CFCATCH TYPE="Database" exception handler. It then resumes with the next statement after the CFTRY block, once the CFCATCH TYPE="Database" handler completes.

Similarly, the CFCATCH TYPE="MissingInclude" block handles exceptions raised by the CFINCLUDE tag. Any unknown, but possibly recoverable, exceptions are handled by the CFCATCH TYPE="Any" block.

<!--- Wrap code you want to check in a CFTRY block --->

<CFTRY>
    <CFQUERY NAME="test" DATASOURCE="company">
        SELECT DepartmentID, FirstName, LastName
        FROM employees
        WHERE employeeID=#EmpID#
    </CFQUERY>

    <HTML>
    <HEAD>
        <TITLE>Test CFTRY/CFCATCH</TITLE>
    </HEAD>

    <BODY>
    <HR>
    <CFINCLUDE TEMPLATE="includeme.cfm">
    <CFOUTPUT QUERY="test">
    <P>Department: #DepartmentID#
    <P>Last Name: #LastName#
    <P>First Name: #FirstName#
    </CFOUTPUT>

    <HR>

<!--- Use CFCATCH to test for missing included files. 
        Print Message and Detail error messages. --->

    <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>
    </CFCATCH>

<!--- Use CFCATCH to test for database errors.
        Print error messages. --->

    <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>
    </CFCATCH>

<!--- Use CFCATCH with TYPE="Any"
    to find unexpected exceptions. --->

    <CFCATCH TYPE="Any">
    <H1>Other Error: #CFCATCH.Type#</H1>

    <CFOUTPUT>
    <UL>
    <LI><b>Message:</b> #CFCATCH.message#
    <LI><b>Detail:</b> #CFCATCH.Detail#
    </UL>
    </CFOUTPUT>
    </CFCATCH>
</CFTRY>
    </BODY>
    </HTML>

CFTHROW syntax

Use CFTHROW within a CFTRY block to raise an error condition. The CFCATCH block can access this message through CFCATCH.message.

<CFTHROW MESSAGE="...diagnostic message...">

This form of the CFTHROW tag throws a new CFML-recoverable exception with the specified diagnostic message.

Using CFTHROW without the MESSAGE attribute throws a new CFML-recoverable exception with an empty diagnostic message.

CFTRY syntax

The CFTRY tag starts a ColdFusion exception-handling block. One or more CFCATCH tags must be included within a CFTRY block.

<CFTRY>
    ...Other CFML tags...

    <CFCATCH TYPE="Any">
</CFTRY>

Note

A CFCATCH block must be the last set of tags within a CFTRY block.

CFCATCH syntax

The CFCATCH tag catches exceptions of the type specified in the TYPE attribute, such as database, application, missing include, or application page.

<CFCATCH TYPE="exception type">

The following form of the CFCATCH tag catches all CFML-recoverable exceptions generated within the preceding CFTRY block, or within any of the CFTRY block's children.

<CFCATCH TYPE="Any">

A CFCATCH tag without a TYPE attribute is equivalent to CFCATCH TYPE="Any".

Order of evaluation

For a given CFTRY block, CFCATCH tags are tested in the order in which they appear in the application page.

Note

An exception raised within a CFCATCH block cannot be handled by the CFTRY block that immediately encloses the CFCATCH tag.

See the CFML Language Reference for information on the syntax of the exception handling tags, CFTRY, CFCATCH, and CFTHROW.


BackUp LevelNext

allaire

AllaireDoc@allaire.com
Copyright © 1998, Allaire Corporation. All rights reserved.