Coldfusion exception not caught by containing try/catch

Viewed 210

You can run this cftry gist with either the first or second indicated block of code uncommented to see the problem.

<cffunction name="alpha" returntype="boolean">
    <cfargument name="boo" type="boolean" />
    <cfreturn arguments.boo />
</cffunction>

<cffunction name="beta">
    <cfset var temp = {} />
    <cftry>
        <cfset temp.userID = 1 />

        <!--- This way throws an *unhandled* exception --->
        <!--- --->
        <cfif alpha(structAppend({userID = temp.userID}, foo))>
            <cfdump var="It worked" />
        </cfif>


        <!--- This way works as expected --->
        <!--- 
        <cfset temp.args = {userID = temp.userID} />
        <cfif alpha(structAppend(temp.args, foo))>
            <cfdump var="It worked" />
        </cfif>
        --->

        <cfcatch>
            <cfdump var="#cfcatch.message#" />
        </cfcatch>
    </cftry>
</cffunction>

I know the struct literal notation {} will sometimes show generically named structs in my debugger, but why should assigning what's created by such syntax make the struct creation happen at a different time than if it's not assigned?

If I debug, I can set a breakpoint on the line where I set temp.userID = 1 and it skips right over it. Moreover, the exception is being thrown on a line contained in a try/catch, but it's failing to catch it.

I know in JavaScript there's the notion of 'hoisting'. I can only assume in some (but not all!) cases CF is doing something similar to struct literals.

Is this a bug, or a known behavior of CF?

0 Answers
Related