Throwing better exceptions with Lucee 6 with cause

One of my favourite little additions with Lucee 6 is being able to pass an exception into a custom throw statement.

For example, if you are doing something in a try catch block, I find myself often wanting to add some additional context to the exception, to be kind to my future self.

The new <CFTHROW> / throw() cause attribute let’s you do this, adding the initial caught exception to the Caused By: section at the bottom of the stack trace.

<cfscript>
	userId=123;
	try {
		// do some stuff which might fail
		throw( message="database constraint violation" );
	} catch ( any e ) {
		// add some useful context to the exception
		throw( message="Error Migrating user [#userId#]", cause=e );
	}
</cfscript>



4 Likes