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 »
11,246 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 »
11,246 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 »
120 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 »
11,246 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 »
11,246 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 »
11,246 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 »
11,246 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
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 23, 2013 at 9:52 PM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Muhmmadibn Did you figure out a solution to launching PDFs? I am running into the same issues myself. There is no way to close the PDF or go back once you launch it. Thanks in advance! ... read »
May 23, 2013 at 6:06 PM
The Girl Who Broke My Heart, And Made Me A Better Person
Good day,ladies and gentle men, my name is Dr AMADI the great spell caster in Africa, i have help so many people for different kind of problems,who say there is no solution to problems on earth, that ... read »
May 23, 2013 at 4:26 PM
ColdFusion QueryAppend( qOne, qTwo )
@Heather, Glad people are still getting value out of this! ... read »
May 23, 2013 at 3:49 PM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, I meant the code at the bottom (not the video). I did try to experiment with an intermediary variable, like: value = users.id[ i ]; arrayContains( userIDs, value ); ... but t ... read »
May 23, 2013 at 11:06 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Are you talking about As Number: YES As String: YES As Java: YES? If so, that's with 3 different ways of referencing the constant 1, not users.id[1]. Query object references(*) are what seem ... read »
May 23, 2013 at 9:55 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Dan, According to the CF Admin, I'm running Java "1.6.0_45". As far as the DB column, in the database it's an INT. I'll see if I can dig into what CF sees it as. @WebManWalking, But h ... read »
May 23, 2013 at 9:49 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, I think the problem is that we're used to loose typing in ColdFusion, like JavaScript. If a value is a number but it's needed in an expression to be a string, noooo problem. I've encountered ... read »
May 23, 2013 at 9:47 AM
ColdFusion QueryAppend( qOne, qTwo )
You rock! Thank you, thank you, thank you!!! ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools