Skip to main content
Ben Nadel at cf.Objective() 2010 (Minneapolis, MN) with: Doug Hughes and Ezra Parker and Dan Wilson and John Mason and Jason Dean and Luis Majano and Mark Mandel and Brian Kotek and Wil Genovese and Rob Brooks-Bilson and Andy Matthews and Simeon Bateman and Ray Camden and Chris Rockett and Joe Bernard and Dan Skaggs and Byron Raines and Barney Boisvert and Simon Free and Steve 'Cutter' Blades and Seth Bienek and Katie Bienek and Jeff Coughlin
Ben Nadel at cf.Objective() 2010 (Minneapolis, MN) with: Doug Hughes Ezra Parker Dan Wilson John Mason Jason Dean Luis Majano Mark Mandel Brian Kotek Wil Genovese Rob Brooks-Bilson Andy Matthews Simeon Bateman Ray Camden Chris Rockett Joe Bernard Dan Skaggs Byron Raines Barney Boisvert Simon Free Steve 'Cutter' Blades Seth Bienek Katie Bienek Jeff Coughlin

Exception Types In CFCatch Blocks Do Not Have To Be Quoted

By
Published in Comments (1)

This is a really minor post, but this morning, I made a small syntax discovery regarding ColdFusion's CFTry / CFCatch statements - the error types do not have to be quoted. This is true even if the type contains a dot-delimited hierarchy. Here's a quick example:

<cfscript>

	try {

		throw( type = "Foo" );

	} catch ( "Foo" error ) {

		writeOutput( "Caught Quoted: #error.type# <br />" );

	}

	try {

		throw( type = "Foo.Bar" );

	} catch ( "Foo.Bar" error ) {

		writeOutput( "Caught Quoted: #error.type# <br />" );

	}


	// ------------------------------------------------------ //
	// Type value not quoted in CFCatch.
	// ------------------------------------------------------ //


	try {

		throw( type = "Foo" );

	} catch ( Foo error ) {

		writeOutput( "Caught Unquoted: #error.type# <br />" );

	}

	try {

		throw( type = "Foo.Bar" );

	} catch ( Foo.Bar error ) {

		writeOutput( "Caught Unquoted: #error.type# <br />" );

	}

</cfscript>

Notice that the type in the Catch block is only quoted in the first two Try/Catch statements, but not in the second two. When we run the above code, we get the following output:

Caught Quoted: Foo
Caught Quoted: Foo.Bar
Caught Unquoted: Foo
Caught Unquoted: Foo.Bar

All catch blocks worked as expected. Pretty cool!

Want to use code from this post? Check out the license.

Reader Comments

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel
Managed hosting services provided by:
xByte Cloud Logo