Skip to content
July 8, 2005 / dshuck

Error Handling in CFSCRIPT

I seem to find myself slowly moving more and more towards using cfscript wherever I can.  I can often create the same end result in fewer lines.  Plus, the code that I am writing is much more in line with other languages.  The problem is when you get down in the cfscript and realize there is no script equivalent for the CF tag you would normally use at a certain point in your logic.  This holds true with <CFTHROW>.  In MX, Macromedia did provide the ability to do try(s) and catch(s).  The one thing they left out (unless I simply can’t find it) was including a throw.  Seems obvious enough huh?

Well, in case you haven’t thought of this pretty obvious solution, I take the approach of adding the following function in my application base classes so that other classes that extend the base class can use it too:

<cffunction name=”throw” output=”false” returntype=”void” access=”public”>
<cfargument default=”" type=”string” name=”message” />
<cfthrow message=”#arguments.message#” />
</cffunction>

Now, anywhere you need it you can just use:
throw(“This is my big error message!”);

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.