Custom Exception Handling

In the previous section, we covered the basics of using CFTRY/CFCATCH to handle predefined types of exceptions that might occur within a ColdFusion application. While these general exception types can handle any type of exception, they do so in a generic, one-size-fits-all manner. Fortunately, ColdFusion also allows you to specify custom exception types that can be caught with the same CFTRY/CFCATCH techniques we already covered.

As I mentioned in the previous section, the CFCATCH tag can accept a custom exception type for its TYPE attribute. That custom exception type is defined using the CFTHROW tag. The CFTHROW tag generates a custom exception type that can be caught by a CFCATCH tag when the TYPE attribute is set to Application, Any, or the custom type you specified in the CFTHROW tag. In other words, when something goes wrong in your application, and you need to generate a custom exception, use CFTHROW. The general syntax for using CFTHROW is as follows:

<CFTHROW TYPE="custom_exception_type"
         MESSAGE="error_message"
         DETAIL="detailed_event_description"
         ERRORCODE="error_code"
         EXTENDEDINFO="extended_information">

The CFTHROW tag accepts the following optional attributes:

TYPE

Specifies a name for the exception type. You may give the exception type a custom name or use the predefined type Application.

MESSAGE

Message describing the event that triggered the exception.

DETAIL

Specifies additional information about the exception.

ERRORCODE

A custom ...

Get Programming ColdFusion now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.