2 Replies Latest reply on May 30, 2014 8:41 AM by PhilBro

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

    PhilBro

      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

        • 1. Re: Why is my try/catch and onError() failing to handle invalid CFML construct errors?
          BKBK Adobe Community Professional & MVP

          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.

          • 2. Re: Why is my try/catch and onError() failing to handle invalid CFML construct errors?
            PhilBro Level 1

            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.