Handling errors with ColdFusion and Flash

To help with debugging, use cftry and cfcatch tags in your ColdFusion page or component to return error messages to Flash Player. For example, the ColdFusion page, causeError.cfm, contains the code:

<cftry> 
    <cfset dev = Val(0)> 
    <cfset Flash.Result = (1 / dev)> 
    <cfcatch type = "any"> 
        <cfthrow message = "An error occurred in this service: #cfcatch.message#"> 
    </cfcatch> 
</cftry>

The second cfset tag in this example fails because it tries to divide by zero (0). The message attribute of the cfthrow tag describes the error; ColdFusion returns this attribute to the SWF application.

To handle the error in your SWF application, create a fault handler like causeError_Fault in the following example:

import mx.remoting.*; 
import mx.services.Log; 
import mx.rpc.*;  
 
// Connect to service and create service object 
    var CFMService:Service = new Service( 
            "http://localhost/flashservices/gateway", 
            null, 
            "helloExamples", 
            null,  
            null ); 
// Call the service causeError() method 
var pc:PendingCall = CFMService.causeError(); 
// Tell the service what methods handle result and fault conditions 
pc.responder = new RelayResponder( this, "causeError_Result", "causeError_Fault" ); 
 
function causeError_Result(re:ResultEvent) 
{ 
    // Display successful result 
    messageDisplay.text = re.result; 
} 
 
function causeError_Fault(fe:FaultEvent) 
{ 
    // Display fault returned from service 
    trace("Error message from causeError is: " + fe.fault.description); 
}

This example displays the trace message from the causeError_Fault function in the Flash Output panel. The portion of the message that is contained in fe.fault.description is the portion of the message that is contained in #cfcatch.message# in the causeError.cfm page.

Note: When you create a ColdFusion page that communicates with Flash, ensure that the ColdFusion page works before using it with Flash.