ColdFusion中的cfcatch的类型 [英] Type of cfcatch in ColdFusion
问题描述
我只是使用 cftry / cfcatch
块来处理任何异常。采取这个简单的例外:
< 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>
并且上面的代码输出如下:
coldfusion.runtime.DivideByZeroException
NO
YES
YES
我的问题是:
为什么 structKeyExists
cfcatch
不是类型 struct
?
并且在转储 cfcatch
时,它似乎是一个 struct
。
任何建议。
)
添加到Miguel-F对CF的typelessness的意见...根据文档, IsStruct 使用以下规则(强调我的):
是ColdFusion结构或是实现java.lang.Map接口的Java
对象。如果变量中的对象是用户定义的函数(UDF),则返回值
为False。
CFCatch
不符合该条件。因此,为什么 IsStruct
返回false。
如果你转储 cfcatch
对象,并检查类层次结构,你会看到它实际上实现为 java.lang.Exception 的子类:
coldfusion.runtime.DivideByZeroException
coldfusion.runtime.ExpressionException
coldfusion.runtime.NeoException
java.lang.RuntimeException
java.lang.Exception
java.lang.Throwable
java.lang.Object
...不 coldfusion.runtime.struct
即CF结构:
coldfusion.runtime.Struct
coldfusion.util.FastHashtable
coldfusion.util.CaseInsensitiveMap
java.lang.Object
因此,如Miguel-F所说可以像结构(可以是最复杂的对象),技术上不是 CF结构,这解释了为什么IsStruct返回false。
另外,可以使用点表示法访问其属性的原因,如CF结构,可能是因为cfcatch类使用 set_PropertyName_(value)方法,如果一个Java类符合
JavaBeans模式。因此,您可以通过
直接引用该属性来设置或获取属性,而不必显式调用方法。
例如,您可以使用以下命令访问cfcatch的message属性:
cfcatch.message
..而不是调用该属性的getter方法:
cfcatch.getMessage()
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.
解决方案 (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):
Returns True, if variable is a ColdFusion structure or is a Java
object that implements the java.lang.Map interface. The return value
is False if the object in variable is a user-defined function (UDF).
CFCatch
does not meet that criteria. Hence why IsStruct
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:
coldfusion.runtime.DivideByZeroException
coldfusion.runtime.ExpressionException
coldfusion.runtime.NeoException
java.lang.RuntimeException
java.lang.Exception
java.lang.Throwable
java.lang.Object
... not coldfusion.runtime.struct
ie CF structure:
coldfusion.runtime.Struct
coldfusion.util.FastHashtable
coldfusion.util.CaseInsensitiveMap
java.lang.Object
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:
ColdFusion can automatically invoke get_PropertyName_() and
set_PropertyName_(value) methods if a Java class conforms to the
JavaBeans pattern. As a result, you can set or get the property by
referencing it directly, without having to explicitly invoke a method.
So for example, you can access the "message" property of cfcatch using:
cfcatch.message
.. instead of invoking its "getter" method for that property:
cfcatch.getMessage()
这篇关于ColdFusion中的cfcatch的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!