cftry cfcatch

Description

Used with one or more cfcatch tags, the cftry tag lets you catch and process exceptions in ColdFusion pages. Exceptions include events that disrupt the normal flow of instructions in a ColdFusion page, such as failed database operations, missing include files, and developer-specified events.

Category

Exception handling tags

Syntax


<cftry>

... Add code here

<cfcatch type = "exceptiontype">

... Add exception processing code here

</cfcatch>

... Additional cfcatch blocks go here

</cftry> 

See also

cferror, cfrethrow, cfthrow

Attributes

Attribute
Description
type
Optional. Specifies the type of exception to be handled by the cfcatch block. The following lists basic exception types you can use:
  • Application
  • Database
  • Template
  • Security
  • Object
  • missingInclude
  • Expression
  • Lock
  • Custom_type
  • Any (default)
For a list of advanced exception types, see "Advanced Exception types".

Usage

You must code at least one cfcatch tag within a cftry block. Code cfcatch tags at the end of the cftry block. ColdFusion tests cfcatch tags in the order in which they appear on the page.

If you specify the type as Any, do so in the last cfcatch tag in the block so that all other tests are executed first.


Note

Specifying the type as Any causes the ColdFusion Application Server to catch exceptions from any CFML tag, data source, or external object, which your application may not be prepared to handle.


Applications can optionally use the cfthrow tag to raise custom exceptions. Such exceptions are caught with any of the following type specifications:

The custom_type type designates the name of a user-defined type specified with a cfthrow tag. cfcatch has a catch handler that can catch a custom type by pattern, if the custom type is defined as a series of strings concatenated by periods, as in "MyApp.BusinessRuleException.InvalidAccount". The cfcatch tag searches for a custom type match, starting with the most specific, and ending with the least specific. For example, you could define a type as follows:

<cfthrow type = "MyApp.BusinessRuleException.InvalidAccount">

cfcatch first searches for the entire type string defined in the cfthrow tag, as follows:

<cfcatch type = "MyApp.BusinessRuleException.InvalidAccount"> 

Then it searches for the next most specific:

<cfcatch type = "MyApp.BusinessRuleException"> 

Finally, it searches for the least specific:

<cfcatch type = "MyApp"> 

The order in which you code cfcatch tags to catch a custom exception type within an application does not matter. A cfcatch tag searches for the custom exception types from most specific to least specific.

If you specify the type as "Application," the cfcatch tag catches only custom exceptions that have the Application type in the cfthrow tag that defines them.

The cfinclude, cfmodule, and cferror tags throw an exception of type = "template".

An exception raised within a cfcatch block cannot be handled by the cftry block that immediately encloses the cfcatch tag. However, you can rethrow the currently active exception with the cfrethrow tag.

You can use the cfcatch variable to access exception the following information:

Advanced Exception types

The following table lists advanced exception types you can specify in the type attribute.
ColdFusion Advanced Exception Types 
COM.Allaire.ColdFusion.CFEXECUTE.OutputError
COM.Allaire.ColdFusion.CFEXECUTE.Timeout
COM.Allaire.ColdFusion.FileException
COM.Allaire.ColdFusion.HTTPAccepted
COM.Allaire.ColdFusion.HTTPAuthFailure
COM.Allaire.ColdFusion.HTTPBadGateway
COM.Allaire.ColdFusion.HTTPBadRequest
COM.Allaire.ColdFusion.HTTPCFHTTPRequestEntityTooLarge
COM.Allaire.ColdFusion.HTTPCGIValueNotPassed
COM.Allaire.ColdFusion.HTTPConflict
COM.Allaire.ColdFusion.HTTPContentLengthRequired
COM.Allaire.ColdFusion.HTTPContinue
COM.Allaire.ColdFusion.HTTPCookieValueNotPassed
COM.Allaire.ColdFusion.HTTPCreated
COM.Allaire.ColdFusion.HTTPFailure
COM.Allaire.ColdFusion.HTTPFileInvalidPath
COM.Allaire.ColdFusion.HTTPFileNotFound
COM.Allaire.ColdFusion.HTTPFileNotPassed
COM.Allaire.ColdFusion.HTTPFileNotRenderable
COM.Allaire.ColdFusion.HTTPForbidden
COM.Allaire.ColdFusion.HTTPGatewayTimeout
COM.Allaire.ColdFusion.HTTPGone
COM.Allaire.ColdFusion.HTTPMethodNotAllowed
COM.Allaire.ColdFusion.HTTPMovedPermanently
COM.Allaire.ColdFusion.HTTPMovedTemporarily
COM.Allaire.ColdFusion.HTTPMultipleChoices
COM.Allaire.ColdFusion.HTTPNoContent
COM.Allaire.ColdFusion.HTTPNonAuthoritativeInfo
COM.Allaire.ColdFusion.HTTPNotAcceptable
COM.Allaire.ColdFusion.HTTPNotFound
COM.Allaire.ColdFusion.HTTPNotImplemented
COM.Allaire.ColdFusion.HTTPNotModified
COM.Allaire.ColdFusion.HTTPPartialContent
COM.Allaire.ColdFusion.HTTPPaymentRequired
COM.Allaire.ColdFusion.HTTPPreconditionFailed
COM.Allaire.ColdFusion.HTTPProxyAuthenticationRequired
COM.Allaire.ColdFusion.HTTPRequestURITooLarge
COM.Allaire.ColdFusion.HTTPResetContent
COM.Allaire.ColdFusion.HTTPSeeOther
COM.Allaire.ColdFusion.HTTPServerError
COM.Allaire.ColdFusion.HTTPServiceUnavailable
COM.Allaire.ColdFusion.HTTPSwitchingProtocols
COM.Allaire.ColdFusion.HTTPUnsupportedMediaType
COM.Allaire.ColdFusion.HTTPUrlValueNotPassed
COM.Allaire.ColdFusion.HTTPUseProxy
COM.Allaire.ColdFusion.HTTPVersionNotSupported
COM.Allaire.ColdFusion.POPAuthFailure
COM.Allaire.ColdFusion.POPConnectionFailure
COM.Allaire.ColdFusion.POPDeleteError
COM.Allaire.ColdFusion.Request.Timeout
COM.Allaire.ColdFusion.SERVLETJRunError
COMCOM.Allaire.ColdFusion.HTTPConnectionTimeout


Note

To see the tag stack displayed by TagContext, use the ColdFusion Administrator to enable the CFML stack trace. Under Debugging in the ColdFusion Administrator, select the checkbox next to "Enable CFML stack trace. "


Example

<!--- cftry example, using TagContext to display the tag stack. --->

<html>

<head>

<title> cftry Example </title>

</head>



<BASEFONT face = "Arial, Helvetica" size = 2>



<body bgcolor = "#FFFFD5">

<H3>cftry Example</H3>



<!--- open a cftry block --->

<cftry>

<!--- note that we have misspelled the tablename

"employees" as "employeeas" --->

<cfquery name = "TestQuery" dataSource = "cfsnippets">

SELECT *

FROM EMPLOYEEAS

</cfquery>



<P>... other processing goes here



<!--- specify the type of error for which we are fishing --->

<cfcatch type = "Database">

<!--- the message to display --->

  <H3>You've Thrown a Database <B>Error</B></H3>

<cfoutput>

<!--- and the diagnostic message from the ColdFusion server --->

  <P>#cfcatch.message#</P>

  <P>Caught an exception, type = #cfcatch.type# </P>

  <P>The contents of the tag stack are:</P>

  <cfloop index = i from = 1 to = #ArrayLen(cfcatch.tagContext)#>

     <cfset sCurrent = #cfcatch.tagContext[i]#>

       <BR>#i# #sCurrent["ID"]# 

(#sCurrent["LINE"]#,#sCurrent["COLUMN"]#) 

#sCurrent["TEMPLATE"]#

  </cfloop>

</cfoutput>

</cfcatch>



</cftry>



</body>

</html>    



Banner.Novgorod.Ru