Friday, 8 August 2014

More on <cfcatch>: Java exceptions

G'day:
This is a very quick appendix to this morning's article "<cfcatch>: my ignorance is reduced. Over a decade after it should have".

How about java exceptions? Well ColdFusion and Railo behave differently here.



This is something Sean prompted me to check (via IRC). Here's simialr code to before which handles a Java exception:

//javaExceptions.cfm
illegalArgumentException = createObject("java", "java.lang.IllegalArgumentException").init("test exception");

try {
    throw(object=illegalArgumentException);
} catch(java e){
    writeOutput("caught it @ java");
} catch(java.lang e){
    writeOutput("caught it @ java.lang");
} catch(java.lang.IllegalArgumentException e){
    writeOutput("caught it @ java.lang.IllegalArgumentException");
} catch (any e){
    writeOutput("caught it @ any");
}
writeOutput("<hr>");

try {
   throw(object=illegalArgumentException);
} catch(java.lang e){
    writeOutput("caught it @ java.lang");
} catch (any e){
    writeOutput("caught it @ any");
}
writeOutput("<hr>");

try {
   throw(object=illegalArgumentException);
} catch(java.lang.IllegalArgumentException e){
    writeOutput("caught it @ java.lang.IllegalArgumentException");
} catch (any e){
    writeOutput("caught it @ any");
}
writeOutput("<hr>");

Here we throw a Java exception instead of a CFML one, and see what does and does not catch it, through various tests.

On CF (I tested on CF9.0.2 -> 11), we get this:

caught it @ java.lang.IllegalArgumentException


caught it @ any

caught it @ java.lang.IllegalArgumentException


And on Railo we get better results:

caught it @ java


caught it @ java.lang

caught it @ java.lang.IllegalArgumentException



Good on Railo.

Oh, and for once my test code ran on OpenBD. It doesn't do a great job:

caught it @ any


caught it @ any

caught it @ any


Anyway, I think if the "namespacing" thing works on ColdFusion for CFML exceptions, it should on Java ones too? What do you think? Raise a ticket?

Righto.

--
Adam