问题描述
我使用ColdFusion 8.我想在ColdFusion中捕获一个 NoClassDefFoundError
异常,但我不能...它仍然失败,并记录错误的异常.log文件。这是我尝试的。
I am using ColdFusion 8. I would like to catch a NoClassDefFoundError
exception in ColdFusion however I can't... It still fails and logs the error in the exception.log file. Here is what I tried.
<cftry>
<cfset myJavaObject.myMethod()>
<cfcatch type="any">
<cfdump var="#cfcatch #">
</cfcatch>
<cfcatch type="java.lang.Throwable">
Horrible exception.
<cfdump var="#cfcatch #">
</cfcatch>
</cftry>
但这不起作用。你能告诉我怎么做吗?我需要在特定的地方而不是在我的Application.cfc的 OnError
函数中捕获此错误。
But this does not work. Could you please show me how to do that? I need to catch this error at a particular place and not in the OnError
function of my Application.cfc.
推荐答案
现在我已经有更多的咖啡,我不认为能够捕获 NoClassDefFoundError
。根据文档,它仅处理:
Now that I have had more coffee, I do not think cfcatch is capable of catching a NoClassDefFoundError
. According to the documentation, it only processes Exceptions:
NoClassDefFoundError
是。
听起来像 cfcatch
只是为了处理正常的可恢复问题。一旦你得到一个 NoClassDefFoundError
,你真的没有什么可以做。这是一个严重的错误,你不能经过它(在正常情况下)。你能做的最多的是显示一条错误信息并退出。
It sounds like cfcatch
was only designed to handle normal "recoverable" problems. There is really not much you can do once you get a NoClassDefFoundError
. It is a severe error and you cannot get past it (under normal circumstances). The most you can do is show an error message and exit.
似乎处理未捕获的错误,如 NoClassDefFoundError
以及异常。所以我认为最好的做法是实现 onError
并显示一个错误页面。
Application.onError
seems to handle uncaught Errors like NoClassDefFoundError
, as well as Exceptions. So I think the best you can do is implement onError
and have it display an error page.
<!---- test code --->
<cfset myJavaObject = createObject("java", "path.to.MyClass") />
<cfset myJavaObject.myMethod() />
<!---- Application.cfc --->
<cfcomponent>
.... settings ...
<cffunction name="onError" returnType="void">
<cfargument name="Exception" required="true" />
<cfargument name="EventName" type="string" required="true" />
<h1>onError Test</h1>
<cfdump var="#Exception#" />
</cffunction>
</cfcomponent>
// test class
public class MyClass {
public void myMethod() {
throw new NoClassDefFoundError ("Testing...");
}
}
更新
文档说,捕获 Throwable
在我的任何测试(或你的)不工作。这强烈建议在行为或文档中的错误。无论如何,它不是工作作为广告,如上所述,唯一的选择我知道是使用一个一般的错误处理程序。如果由于某种原因您必须坚持使用Application.cfm文件,请尝试使用
Despite what the documentation says, catching Throwable
does not work in any of my tests (or yours). That strongly suggests a bug in the behavior or the documentation. Either way it does not work as advertised, so as mentioned above, the only alternative I know of is using a general error handler. If you must stick with an Application.cfm file for some reason, try using <cferror type="exception" ...>
(Absurd )测试用例:
<cftry>
<cfset myJavaObject = createObject("java", "path.to.MyClass")>
<cfset myJavaObject.myMethod()>
<cfcatch type="java.lang.NoClassDefFoundError">
CAUGHT java.lang.NoClassDefFoundError
</cfcatch>
<cfcatch type="java.lang.LinkageError">
CAUGHT java.lang.LinkageError
</cfcatch>
<cfcatch type="java.lang.Error">
CAUGHT java.lang.Error
</cfcatch>
<cfcatch type="java.lang.Throwable">
CAUGHT java.lang.Throwable
</cfcatch>
<cfcatch type="any">
CAUGHT ANY
</cfcatch>
<cfcatch>
CAUGHT
</cfcatch>
</cftry>
这篇关于ColdFusion不捕获NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!