Skip to main content
April 27, 2006
Question

cftry/cfcatch issue

  • April 27, 2006
  • 1 reply
  • 670 views
Hi,

Cfcatch does not seem to catch all errors. Below are two examples of errors that it will not catch. Am I doing something wrong or are there some limitations on this tag? BTW I don't think that cferror will catch them either although sitewide error handling does seem to catch them.

    This topic has been closed for replies.

    1 reply

    BKBK
    Community Expert
    Community Expert
    April 28, 2006
    "Errors" is the generic name but, technically, there is a distinction between the categories of error. The two categories that concern us here are "Syntax errors" and "Exceptions".

    Coldfusion, like most programming languages, begins by parsing the code. If it finds a mistake in the Coldfusion syntax or grammar, it will stop and signal it to you. Those are errors which the parser identifies as "bad Coldfusion" or "non-Coldfusion". An example is <cfsat x=1>. Coldfusion doesn't continue past the first syntax error. No need.

    Coldfusion would only execute a piece of code if it found no syntax errors in it during the parsing phase. "Exception" is the general name for an error that occurs during the execution phase. An example is <cfset x=1/0>. Though it is division by zero, the statement has correct Coldfusion syntax.

    The try-catch mechanism is meant to trap exceptions. As such, it only comes into effect during code execution.

    Your code contains two syntax errors. First, <cfset 11 = 1> is not a valid Coldfusion statement. Second, "2/31/06" is not a valid date object and cannot be cast into one. Coldfusion will therefore not even execute the code, and so the try-catch will not have the opportunity to run.

    An example of code that will throw and catch exceptions:

    April 28, 2006
    BKBK

    Thank you very much for clarifying this for me.

    What then, would be a good way to throw a friendly error for the user when they enter an invalid date, such as: 2/31/06?
    Inspiring
    April 28, 2006
    You can validate your input with ColdFusion by placing a tag like this in your form:

    <INPUT TYPE="hidden" NAME="SomeDate_date" VALUE="Enter valid date.">

    This would require the form field SomeDate to have a date value. You can also check when you process the form with the IsDate() function.