ColdFusion won't catch NoClassDefFoundError

I am using ColdFusion 8. I would like to catch the NoClassDefFoundError exception in ColdFusion, but I cannot ... It still fails and logs the error in the exception.log file. Here is what I have tried.

 <cftry> <cfset myJavaObject.myMethod()> <cfcatch type="any"> <cfdump var="#cfcatch #"> </cfcatch> <cfcatch type="java.lang.Throwable"> Horrible exception. <cfdump var="#cfcatch #"> </cfcatch> </cftry> 

But that does not work. Could you show me how to do this? I need to catch this error in a specific place, and not in the OnError function of my Application.cfc.

+6
source share
1 answer

Now that I had more coffee, I don't think cfcatch is capable of catching a NoClassDefFoundError . According to the documentation, it processes only Exceptions :

Exceptions are events that disrupt the normal flow of instructions on the ColdFusion page, for example, failed database operations, missing files and events specified by the developer.

NoClassDefFoundError is Error .

The error indicates serious problems that a reasonable application should not be attempted to catch.

It seems that cfcatch was only intended to handle the usual "recoverable" problems. There is really little that can be done once you get a NoClassDefFoundError . This is a serious mistake, and you cannot pass by it (under normal circumstances). The most you can do is show the error message and exit.

Application.onError seems to handle NoClassDefFoundError errors such as NoClassDefFoundError as well as Exceptions. Therefore, I think the best you can do is implement onError and display the 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..."); } } 

Update

Any type includes all the error with the Java object type 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

Despite what the documentation says, catch Throwable does not work in any of my tests (or yours). This strongly indicates a bug in behavior or documentation. In any case, this does not work as advertised, as mentioned above, the only alternative that I know is to use a common error handler. If for some reason you should stick with the Application.cfm file, try using <cferror type="exception" ...>

(Absurd) Test case:

 <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> 
+9
source

All Articles