gpt4 book ai didi

java - ColdFusion 没有捕捉到 NoClassDefFoundError

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:02:50 25 4
gpt4 key购买 nike

我正在使用 ColdFusion 8。我想在 ColdFusion 中捕获 NoClassDefFoundError 异常,但是我不能...它仍然失败并将错误记录在 exception.log 文件中。这是我尝试过的。

<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 函数中。

最佳答案

现在我喝了更多的咖啡,我不认为cfcatch能够捕获 NoClassDefFoundError .根据文档,它只处理 Exceptions :

Exceptions are events that disrupt the normal flow of instructions in a ColdFusion page, such as failed database operations, missing include files, and developer-specified events.

NoClassDefFoundError是一个 Error .

An Error indicates serious problems that a reasonable application should not try to catch

听起来像cfcatch仅设计用于处理正常的“可恢复”问题。一旦获得NoClassDefFoundError,您真的无能为力。 .这是一个严重的错误,你无法通过它(在正常情况下)。您最多只能显示一条错误消息并退出。

Application.onError 似乎可以处理 Uncaught Error ,例如 NoClassDefFoundError ,以及异常。所以我认为你能做的最好的就是实现 onError并让它显示一个错误页面。

    <!---- 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...");
}
}

更新

The Any type includes all error with the Java object type of java.lang.Exception. It does not include java.lang.Throwable errors. To catch Throwable errors, specify java.lang.Throwable in the cfcatch tag type attribute

不管文档怎么说,捕获 Throwable在我的任何测试(或你的)中都不起作用。这强烈表明行为或文档中存在错误。无论哪种方式,它都像宣传的那样工作,所以如上所述,我所知道的唯一选择是使用通用错误处理程序。如果出于某种原因必须坚持使用 Application.cfm 文件,请尝试使用 <cferror type="exception" ...>

(荒谬)测试用例:

<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>

关于java - ColdFusion 没有捕捉到 NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14237283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com