Skip to main content
Participating Frequently
November 4, 2010
Question

Exception Handling Problem

  • November 4, 2010
  • 3 replies
  • 1210 views

I'm trying to write a separate error handler to handle all errors in CFScript.

But, I'm running into two issues:

1. rethrow excp

    produces "Invalid CFML construct".

    In Developing ColdFusion 9 Applications, Ch. 4, pg. 108,

    New tag equivalents in CFScript,

    <cfrethrow>'s CFScript equivalent is "rethrow".

    Do you know if the docmentation is incorrect?

    Did they not get "rethrow" into CF9?

2. What data type are exceptions?

    I'm trying to pass an exception to my error handler as

    an Object, but I get the error:

    "The EXCP argument passed to the errorHandler function is not of type Object."

    However, IsObject(excp)=true in my calling function.

Below is an example of what I'm trying to do.

Thanks,

myscreenname0345

=============================================================

<cfscript>

//-----------------------------------------------------------

public void function myFunc() {

try {

   var i = 1/0;     // Divide by 0 will generate exception.

} catch (any excp) {

   rethrow excp;

}

}

//-----------------------------------------------------------

public void function errorHandler(Object excp) {

variables.scriptFailed = true;

writelog(file="logfile", text="#excp.Message#");

}

//----------------- Main ------------------------------------

scriptFailed = false;

try {

   myFunc();

} catch (any excp) {

   writeOutput(IsObject(excp));

   errorHandler(excp);

} finally {

   writelog(file="logfile", text="#variables.scriptFailed#");

}

</cfscript>

=============================================================

    This topic has been closed for replies.

    3 replies

    Participating Frequently
    November 9, 2010

    Thank you!

    You've solved my problem.

    Stupid me failed to see that rethrow doesn't have an argument!

    Simply "rethrow;" works.

    Also, when I removed explicitly specifying the argument type in the function,

       public void errorHandler(excp)

    I am no longer getting any errors, and it works.

    I'm surprised it allowed me to get away without specifying a type

    but since it's generally a type-less language, I guess that explains it.

    Thanks very much!

    myscreenname0345

    Inspiring
    November 13, 2010
    I'm surprised it allowed me to get away without specifying a type

    It probably defaults to "Any" and is equivalent to saying:

                  public void errorHandler(Any excp)

    BKBK
    Community Expert
    November 13, 2010

    -==cfSearching==- wrote:

    It probably defaults to "Any"

    Indeed. Most, if not all, types in Coldfusion default to any. Weak typing can be quite handy.

    Inspiring
    November 6, 2010

    1. rethrow excp

        produces "Invalid CFML construct".

    Like CFRethrow, it does not accept an argument. It is just: rethrow;

    2. What data type are exceptions?

        I'm trying to pass an exception to my error handler as

        an Object, but I get the error:

        "The EXCP argument passed to the errorHandler function is not of type Object."

    IIRC you use the same types as you would with CFArgument. Only certain values are allowed (string, any, component name, etc..). "Object" is not one of the allowed types. So CF probably thinks "Object" is a component name and that is why the ".. not of type" error occurs. I believe exceptions fall into the "Any" category.

    Message was edited by: -==cfSearching==-

    BKBK
    Community Expert
    November 8, 2010

    -==cfSearching==- wrote:

    1. rethrow excp

        produces "Invalid CFML construct".

    Like CFRethrow, it does not accept an argument. It is just: rethrow;

    Ah, naturally. Rang a bell after you said it. The new scripting takes some getting used to, especially for a tag man.

    2. What data type are exceptions?

        I'm trying to pass an exception to my error handler as

        an Object, but I get the error:

        "The EXCP argument passed to the errorHandler function is not of type Object."

    IIRC you use the same types as you would with CFArgument. Only certain values are allowed (string, any, component name, etc..). "Object" is not one of the allowed types. So CF probably thinks "Object" is a component name and that is why the ".. not of type" error occurs. I believe exceptions fall into the "Any" category.

    I still find passing argument-types quite tricky, even before CF9 came along. Coldfusion is weakly-typed, but is able to enforce type-checking here and there. As a result, its type-checking can be confusing or inconsistent.

    For example, the rules say you may declare a custom variable name as argument type.  So, suppose Myscreenname0345 were to do something like this instead.

    <cfcomponent>
    <cfset obj = createobject("java","java.lang.Object")>
    <cffunction name="errorHandler" access="public" returntype="void">
    <cfargument name="excp" type="obj">
    <cfset variables.scriptFailed = true>
    <cflog file="logfile" text="#excp.Message#">
    </cffunction>
    </cfcomponent>

    My feeling is that Coldfusion will still complain that the argument passed isn't of type obj. I suppose this is because type-checking should occur at compile-time but Coldfusion, being weakly-typed, does type-checking later at runtime.

    The types we're dealing with here are just a limited amount of the compile-time type-checking Coldfusion does, for example for arguments, catch, and so on. We should not expected this to include every Java class we care to define.

    Inspiring
    November 8, 2010

    For example, the rules say you may declare a custom

    variable name as argument type. 

    No, by "variableName" I think they just mean the value should conform to the general rules for safe variable names. ie Should start with a letter, only contain letters/digits/underscores, etcetera...

    BKBK
    Community Expert
    November 6, 2010

    myscreenname0345 wrote:

    I'm trying to write a separate error handler to handle all errors in CFScript.

    But, I'm running into two issues:

    1. rethrow excp

        produces "Invalid CFML construct".

        In Developing ColdFusion 9 Applications, Ch. 4, pg. 108,

        New tag equivalents in CFScript,

        <cfrethrow>'s CFScript equivalent is "rethrow".

        Do you know if the docmentation is incorrect?

        Did they not get "rethrow" into CF9?

    2. What data type are exceptions?

        I'm trying to pass an exception to my error handler as

        an Object, but I get the error:

        "The EXCP argument passed to the errorHandler function is not of type Object."

        However, IsObject(excp)=true in my calling function.

    Below is an example of what I'm trying to do.

    Thanks,

    myscreenname0345

    =============================================================

    <cfscript>

    //-----------------------------------------------------------

    public void function myFunc() {

    try {

       var i = 1/0;     // Divide by 0 will generate exception.

    } catch (any excp) {

       rethrow excp;

    }

    }

    //-----------------------------------------------------------

    public void function errorHandler(Object excp) {

    variables.scriptFailed = true;

    writelog(file="logfile", text="#excp.Message#");

    }

    //----------------- Main ------------------------------------

    scriptFailed = false;

    try {

       myFunc();

    } catch (any excp) {

       writeOutput(IsObject(excp));

       errorHandler(excp);

    } finally {

       writelog(file="logfile", text="#variables.scriptFailed#");

    }

    </cfscript>

    =============================================================

    I'm not aware that the function rethow() exists. Use thow().

    For example,

    <cfscript>
    public void function myFunc() {
        try {
               var i = 1/0;     // Divide by 0 will generate exception.
        } catch (any excp) {
               throw excp;
        }
    }
    public void function errorHandler(excp) {
        writeOutput("Exception class of excp: " & excp.getClass() & "<br>");
        variables.scriptFailed = true;
        writelog(file="logfile", text="#excp.Message#");
    }
    scriptFailed = false;
    try {
       myFunc();
    } catch (any excp) {
       writeOutput("IsObject(excp): " & IsObject(excp) & "<br>");
       errorHandler(excp);
    } finally {
       writelog(file="logfile", text="#variables.scriptFailed#");
    }
    </cfscript>