Skip to main content
Known Participant
May 29, 2014
解決済み

Why is my try/catch and onError() failing to handle invalid CFML construct errors?

  • May 29, 2014
  • 返信数 1.
  • 462 ビュー

I was playing around with different ways to set cookies and in the process I use the cfcookie tag wrong. It threw an error and the onError() sort of did its job. Rather than giving a nice clean error like normal, the page completely quit processing.

So I played around some more and tried this with out an onError() in the application.cfc.

<cftry>

    <cfset asdf = />

   

    <cfcatch type="any">

        <cfdump var="#cfcatch#">

    </cfcatch>

</cftry>

It threw the error as if the try/catch wasn't there. Is this normal and it has taken me years to notice it?

Phil

このトピックへの返信は締め切られました。
解決に役立った回答 BKBK

The behaviour you observe is the expected, and correct, one. The purpose of try/catch and onError is to catch exceptions.

ColdFusion compiles pages into executable code before running them. Strictly speaking, exceptions are errors that occur when ColdFusion runs already compiled CFML (CFM and CFC) pages.

However,  what happens when Coldfusion finds errors, for example, programming and syntax errors, during compilation? Such errors occur before ColdFusion converts the code to executable form. You therefore cannot use try/catch or onError to handle them.

Coldfusion will report the errors that it finds during compilation as compiler errors.  That is what happens in your example. The line

  <cfset asdf = />

contains a programming error and so won't even compile. That is why you get a compiler error.

If you replace it with the line,

<cfset asdf = 2/0 />

you will get code that compiles, but which generates an exception.

返信数 1

BKBK
Community Expert
BKBKCommunity Expert解決!
Community Expert
May 30, 2014

The behaviour you observe is the expected, and correct, one. The purpose of try/catch and onError is to catch exceptions.

ColdFusion compiles pages into executable code before running them. Strictly speaking, exceptions are errors that occur when ColdFusion runs already compiled CFML (CFM and CFC) pages.

However,  what happens when Coldfusion finds errors, for example, programming and syntax errors, during compilation? Such errors occur before ColdFusion converts the code to executable form. You therefore cannot use try/catch or onError to handle them.

Coldfusion will report the errors that it finds during compilation as compiler errors.  That is what happens in your example. The line

  <cfset asdf = />

contains a programming error and so won't even compile. That is why you get a compiler error.

If you replace it with the line,

<cfset asdf = 2/0 />

you will get code that compiles, but which generates an exception.

PhilBro作成者
Known Participant
May 30, 2014

I think the 3 60+ hour work weeks managed to shut down the part of my brain with 15+ years of CF development experience. Plus I got some sleep which always helps.