Ask Ben: CFTry / CFCatch Issues

Posted March 15, 2007 at 8:09 AM by Ben Nadel

Tags: ColdFusion, Ask Ben

Someone contacted me the other day with a Try / Catch error. They were having problems with ColdFusion interacting with an Anti-Virus software. The AV software was putting locks on uploaded files which was causing ColdFusion to error out when it tried to access them. The programmer tried to handle this by using a Try / Catch code block, but was still getting errors. This is the snippet of code that I was sent:

  • <cfscript>
  • try {
  • fileRead = createObject( "java","java.io.FileInputStream" );
  • fileRead.init( theFile );
  • } catch (Any e) {
  • errorCode = '99';
  • };
  • fileRead.close();
  • </cfscript>

This code was causing a "java.io.FileInputStream" error. The problem is quite subtle and has to do with the line:

  • fileRead.close();

If you look at the Try / Catch block you will see that the fileRead variable creation is in the Try / Catch, but the fileRead.close() command is outside of the Try / Catch. This means that even though the variable fileRead might not contain a valid File object (if that is where the error occurs), the close() method is called no matter what.

To fix this, all we had to do was move the fileRead.close() to inside of the Try / Catch code block:

  • <cfscript>
  • try {
  • fileRead = createObject( "java","java.io.FileInputStream" );
  • fileRead.init( theFile );
  • fileRead.close();
  • } catch (Any e) {
  • errorCode = '99';
  • };
  • </cfscript>

Now, the fileRead.close() command only gets run if the fileRead variable contains a valid, initialized Java File object.



Reader Comments

Mar 15, 2007 at 8:36 AM // reply »
56 Comments

this is why we need a cffinally tag and finally inside cfscript.


Mar 15, 2007 at 8:44 AM // reply »
10,640 Comments

Can you give an example of how it might be used? I don't see how it would add anything over the CFCatch tag itself?


Mar 15, 2007 at 10:30 AM // reply »
56 Comments

fileRead = createObject( "java","java.io.FileInputStream" );

try {
fileRead.init( theFile );

} catch (Any, e) {
errorCode = '99';
} finally {
fileRead.close();
};

No matter what happens the object will always be closed. With the code you wrote, if the fileRead.init throw an error, it will never make it to the close statement because the exception will be raised. Using finally, the exception is raise, but the object is still close because the finally statement is always executed even if an exception is raised or not.


Mar 15, 2007 at 10:39 AM // reply »
10,640 Comments

Why not just put the "finally" code after the Try / Catch block? This seems like it would accomplish the same thing?


Mar 15, 2007 at 3:10 PM // reply »
105 Comments

If the catch clause did a rethrow, the finally would still be executed (which would not be true of code placed after the try/catch block).


Mar 15, 2007 at 3:23 PM // reply »
10,640 Comments

Ahhh, interesting. I didn't think of that, thanks.


Mar 15, 2007 at 5:01 PM // reply »
1 Comments

Tony,

I gave a try to this code inside a CFscript

} catch (Any e) {
errorCode = '99';
} finally {
fileRead.close();
};

But it returns a syntax error on the finally line.... Is 'finally' part of coldFusion scripting language ? or is it pure Java/javascript? or I still miss something.... :)


Mar 21, 2007 at 1:07 AM // reply »
1 Comments

I think there should be no comma within the catch brackets


Mar 21, 2007 at 7:27 AM // reply »
10,640 Comments

@BKBK,

Thanks, that was a typo. Removed.


Sep 4, 2007 at 8:12 PM // reply »
1 Comments

What's available on the exception (e) ?


Sep 5, 2007 at 8:15 AM // reply »
10,640 Comments

@CP,

The thrown exception has information about the message and detail of the error. It also has the tag context so you can see what template and which lines of code have been executed. And, if you are particularly lost, it should also have the Java stack trace from which you might be able to take little pieces of information.


Feb 1, 2008 at 12:03 PM // reply »
1 Comments

I couldn't agree more with Tony.

We need a FINALLY option for Coldfusion. Java has it. .NET has it.


Apr 10, 2008 at 1:28 PM // reply »
4 Comments

A CFFinally solution:

<cftry>
<!---- Do some work ---->
<cfcatch type="any">
</cfcatch>
</cftry>
<!--- <cffinally> --->
<!--- Do some clean up work --->
<!--- </cffinally> --->
<cfif IsDefined("cfcatch")>
<cfthrow object="#cfcatch#">
</cfif>

One thing to note is that if you encounter an exception during the clean up work you will end up loosing the original exception. You could place the cleanup code around a try catch and log the original exception before throwing the new one. Too bad you can't wrap exceptions around each other.


Apr 10, 2008 at 1:41 PM // reply »
10,640 Comments

@Abc,

I like it. Cool example.



Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 12, 2012 at 3:37 AM
Learning ColdFusion 8: CFImage Part III - Watermarks And Transparency
Hi Ben, Just to ask currently it is placed bottom right corner, if i need to replace the same rendered image on the bottom left side or in the bottom center, how that can be calculated. bottom ce ... read »
Feb 11, 2012 at 9:29 PM
Use jQuery's SlideDown() With Fixed-Width Elements To Prevent Jumping
I can't say how glad I am that I found your post. Thank you very much. ... read »
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »