Dumping CFCATCH

One common things folks may do when using try/catch in CFML is to simply cfdump the CFCATCH object. I know that I, probably like most folks, assumed that the CFCATCH variable was a structure. It looks like a structure (with values like cfcatch.tagcontext) and most x.y things in CF are structures, so it was probably a safe assumption. However, there are times when dumping cfcatch will not result in a nice little struct. Instead you get something like this:

This was generated from the following code:

<FONT COLOR=MAROON><cftry></FONT>
   <FONT COLOR=MAROON><cfset x = structNew()></FONT>
   <FONT COLOR=MAROON><cfoutput></FONT>#x.y#<FONT COLOR=MAROON></cfoutput></FONT>
   <FONT COLOR=MAROON><cfcatch></FONT>
      <FONT COLOR=MAROON><cfdump var=<FONT COLOR=BLUE>"#cfcatch#"</FONT>></FONT>
   <FONT COLOR=MAROON></cfcatch></FONT>
<FONT COLOR=MAROON></cftry></FONT>

What is interesting is that it seems to be only a specific type of error: coldfusion.runtime.UndefinedElementException. If you change the code above, let’s say to foo() (a udf that does not exist), the dump works correctly.

So what to do? You could switch to a more manual approach:

<FONT COLOR=MAROON><cfoutput></FONT>
<FONT COLOR=TEAL><table></FONT>
   <FONT COLOR=TEAL><tr></FONT>
      <FONT COLOR=TEAL><td></FONT>Type:<FONT COLOR=TEAL></td></FONT>
      <FONT COLOR=TEAL><td></FONT>#cfcatch.type#<FONT COLOR=TEAL></td></FONT>
   <FONT COLOR=TEAL></tr></FONT>
   <FONT COLOR=TEAL><tr></FONT>
      <FONT COLOR=TEAL><td></FONT>Message:<FONT COLOR=TEAL></td></FONT>
      <FONT COLOR=TEAL><td></FONT>#cfcatch.message#<FONT COLOR=TEAL></td></FONT>
   <FONT COLOR=TEAL></tr></FONT>
   <FONT COLOR=TEAL><tr></FONT>
      <FONT COLOR=TEAL><td></FONT>Detail:<FONT COLOR=TEAL></td></FONT>
      <FONT COLOR=TEAL><td></FONT>#cfcatch.detail#<FONT COLOR=TEAL></td></FONT>
   <FONT COLOR=TEAL></tr></FONT>
<FONT COLOR=TEAL></table></FONT>
<FONT COLOR=MAROON></cfoutput></FONT>

This works nicely, although for the original error, the detail row is blank. You could also add more detail, like the tag context, etc.

So, this was discussed on cf-guru quite a bit. Sean Corfield made the point that the documentation does not explicitly state that cfcatch is a structure. However, I think it’s current behaviour would be considered a bug. I’d be willing to bet that this particular CF exception has a simple bug in it that makes it not work quite well with cfdump. Either way it is something to keep in mind.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA https://www.raymondcamden.com

Comments