An application may need to throw an error when an actual error has not occurred.
ColdFusion allows an application to throw a custom error using the <cfthrow> tag.
There are instances when an application will need to throw an error when an actual error hasn't occured. ColdFusion allows for this using the <cfthrow> tag. A custom error type and message can specified using the type and message attributes of the <cfthrow> tag.
One case where this may be needed is when a check is made to see if a user exists in an application already. If the user does exist a custom error is thrown:
<cfquery name="qCheckUserByEmail">
SELECT userId
FROM user
WHERE
userEmail = <cfqueryparam
value="#arguments.userEmail#"
cfsqltype="cf_sql_varchar" />
</cfquery>
<cfif qCheckUserByEmail.RecordCount>
<cfthrow
type="USER_EXISTS"
message="The email already exists in the database" />
</cfif>
This new custom error can now be caught the same way any other error is caught using a try/catch block.
<cftry>
<cfset checkUserByEmail( form.userEmail ) />
<cfcatch type="USER_EXISTS">
<!--- Handle Error Here --->
</cfcatch>
</cftry>
The type in the <cfcatch> block must match the custom type from the type attribute of the <cfthrow> tag.
ColdFusion yet again makes the developers life easier by allowing for custom errors to be thrown and caught easily.
+