So, I ran into an interesting issue today. We had some UDFs written in cfscript that used try/catch. The object defined in the cfcatch block was var scoped since... well... we always var scope, right? Consider this code block:
function foo() {
var myerror = "";
x = "apple";
try {
if(x+9) x = 1;
} catch(ANY myerror) {
writeOutput(myerror.message & "<p>");
}
}
</cfscript>
<cfoutput>
#foo()#
</cfoutput>
Everything looks hunkey-dorey, right? However, running this will give you: You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members.
Turns out - the var scope of myerror seems to prevent it from being used in the cfcatch block. If you remove the var scope, not only does the code work right, it won't overwrite a variable called myerrors that may exist in the Varibles scope.


Comment 1 written by Rob Brooks-Bilson on 2 March 2005, at 6:22 PM
Seriously, Ray. Good info here. Thx.
Comment 2 written by John Farrar on 2 March 2005, at 6:29 PM
<cfscript>
function foo() {
var local = structNew();
var x = "apple";
try {
if(x+9) x = 1;
} catch(ANY local.myerror) {
writeOutput(local.myerror.message & "<p>");
}
}
</cfscript>
<cfoutput>
#foo()#
</cfoutput>
Heh... life gets twisted. (I have also learned on "iteration" calls of functions... the "structNew()" can make execution much slower. So this solution is a spare use function.
Comment 3 written by Sean Corfield on 2 March 2005, at 6:57 PM
Comment 4 written by Jeff Coughlin on 2 March 2005, at 6:57 PM
I'm running out the door at the moment, but maybe I'll try this tonight if someone else hasn't.
Comment 5 written by Arindam Biswas on 3 March 2005, at 8:51 AM
Comment 6 written by Raymond Camden on 3 March 2005, at 8:57 AM
Comment 7 written by Nathan Dintenfass on 4 March 2005, at 11:59 AM
Comment 8 written by Raymond Camden on 4 March 2005, at 1:43 PM
Comment 9 written by Roland Collins on 4 March 2005, at 1:54 PM
Even if you declare myerror as "java.lang.Exception", it still misbehaves. Instead of the dereference error, you get a NullPointerException.
Comment 10 written by William on 22 March 2006, at 6:01 PM
Comment 11 written by Raymond Camden on 23 March 2006, at 9:02 AM
[Add Comment] [Subscribe to Comments]