Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Exception Handling Problem

New Here ,
Nov 04, 2010 Nov 04, 2010

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>

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

1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 06, 2010 Nov 06, 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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 06, 2010 Nov 06, 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==-

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 08, 2010 Nov 08, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 08, 2010 Nov 08, 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...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 08, 2010 Nov 08, 2010

-==cfSearching==- wrote:

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...

You see, that's why I find the matter at times confusing and inconsistent. A note about variable names belongs to the usage-notes; it shouldn't be bulleted as one of the possible data types.

The kind of custom data type I described isn't far-fetched. You may in fact pass such a custom variable as the type attribute of catch/cfcatch.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 08, 2010 Nov 08, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 13, 2010 Nov 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)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 13, 2010 Nov 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 14, 2010 Nov 14, 2010
LATEST

Weak typing can be quite handy.

Look Ma' no casting

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources