5

I am trying to use a <cftry> and <cfcatch> block of code. However I am confused about something, I put a <cftry> block around my query and the result of that query is not empty, but the condition always goes into the <cfcatch> block.

Example :

<cftry>
  <cfquery name="qcar" datasource="xxxx">
     select * from allcar
   </cfquery>
<cfcatch>
   <script>
      alert("ERROR");
   </script>
</cfcacth>
</cftry>

What is wrong with this code that it always goes to the <cfcatch>?

3
  • 6
    Have you tried dumping the error? <cfdump var="#cfcatch#" /> will display the full error. This will tell you what caused the error. wikidocs.adobe.com/wiki/display/coldfusionen/cfdump wikidocs.adobe.com/wiki/display/coldfusionen/cfcatch Jul 31, 2015 at 4:06
  • LearnCFInAWeek also has a good tutorial on error handling with examples: learncfinaweek.com/week1/Error_Handling Jul 31, 2015 at 4:08
  • 4
    RE: anybody can tell , what the cause of this error? No. Your code only indicates there was an error - it does tell us anything else about the error. If you want to know what caused the error, you must dump the cfcatch object as beloitdavisja described. You might also want to review the tutorial he posted, because mixing client side (javascript) and server side (CFML) code to handle server errors is not the norm.
    – Leigh
    Jul 31, 2015 at 5:30

2 Answers 2

11

You have a typo in your code, the code in your answer should not even pass the syntax check. Please correct both - the code in your question and the one on your server, and check whether this solves your problem. If this is not the case, please apply a <cfdump var="#cfcatch#" />, like @beloitdavisja told you and show as further error messages. The typo is in line 9, </cfcacth> should be </cfcatch>.

<cftry>
    <cfquery name="qcar" datasource="xxxx">
        select * from allcar
    </cfquery>

    <cfcatch type="any">
        <!--- Your debug output <script>alert("ERROR");</script>--->
        <!--- Debugging - The ColdFusion Way --->
        <cfdump var="#cfcatch#" />
    </cfcatch>
</cftry>
2
  • +1 to give you some more rep so you can comment on here. Also, I would add the <cfdump var="#cfcatch#" /> code to your code example (replacing the <script>alert("ERROR");</script> code that the OP has.
    – Miguel-F
    Jul 31, 2015 at 13:28
  • 1
    Good catch (haha!) on the typo. Jul 31, 2015 at 14:27
5
<cftry>

    <cfquery name="qEmployee" datasource="cfdocexamples">
          SELECT * FROM Employeess
    </cfquery>


 <cfcatch type="any">
       <cfoutput>
          Error occured....<br /><br />
        Message: <b>#cfcatch.Message#</b><br /> 
        Detail: <b>#cfcatch.Detail#</b><br />
        Type: <b>#cfcatch.Type#</b><br />

      </cfoutput>
  </cfcatch>
</cftry>

Note: #cfcatch# give full details in json format

#cfcatch.Message# give you only error message in string format

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.