I am simply using a cftry/cfcatch
block for handling any exception. Take this simple exception:
<cftry>
<cfset abc = 1/0>
<cfcatch>
<cfdump var="#cfcatch.getClass().getName()#">
<cfdump var="#isStruct(cfcatch)#">
<cfdump var="#isObject(cfcatch)#">
<cfdump var="#structKeyExists(cfcatch, 'type')#">
</cfcatch>
</cftry>
And the above code's output is like this:
coldfusion.runtime.DivideByZeroException
NO
YES
YES
My question is:
Why structKeyExists
is not throwing an error as cfcatch
is not of type struct
?
And on dumping cfcatch
it seems like it is a struct
.
Any suggestions.
I think what is confusing you is that you need to remember that ColdFusion is a typeless language.
ColdFusion documentation on data types
So the code within the
<cfcatch>
block contains an object that can be referred to as a "structure". By default the name of that structure iscfcatch
. You can override that name by specifying thename
attribute within the<cfcatch>
tag.The easiest way to see everything that is available to you is to
<cfdump>
the entire structure within the<cfcatch>
block.CFCatch documentation on cfcatch
(Too long for comments..)
Adding to Miguel-F's comments about CF's "typelessness"... according to the documentation, IsStruct uses the following rules (emphasis mine):
CFCatch
does not meet that criteria. Hence whyIsStruct
returns false.If you dump the
cfcatch
object, and examine the class hierarchy, you will see it is actually implemented as a subclass of java.lang.Exception:... not
coldfusion.runtime.struct
ie CF structure:So as Miguel-F said, though it can be used like a structure (as can most complex object), technically it is not a CF structure, which explains why IsStruct returns false.
As an aside, the reason you can access its properties using dot notation, like with CF structures, is probably because the cfcatch class uses the JavaBean pattern:
So for example, you can access the "message" property of cfcatch using:
.. instead of invoking its "getter" method for that property:
cfcatch object acts like a struct, but it is not one. This is a special case.
What you can do is make a duplicate of cfcatch object and try isStruct method on it, it will return true.
for example-
The output would be like