LogoLogo
Printed BookDownload BookCFML SupportCFML Videos
  • Introduction
  • Intro
    • Welcome
      • Author
      • About This Book
    • What is ColdFusion (CFML)
    • CommandBox CLI
    • Instructions & Interpreters
  • CFML Language
    • Syntax
    • Comments
    • Variables
    • Variable Scopes
    • Operators
    • Null & Nothingness
    • Strings
    • JSON
    • Numbers
    • Arrays
    • Structures
    • Database Queries
    • Conditionals
    • Exception Management
    • Components
      • Properties
      • Functions
      • Static Constructs
      • Final Constructs
      • Abstract Constructs
      • Interfaces
    • Closures
    • Code Locking
    • Threading
    • Includes
    • Java Integration
  • Beyond The 100
    • Beyond The 100
    • Application.cfc
    • File Handling
    • Image Manipulation
    • HTTP/S Calls
    • Sending Emails
    • Asynchronous Programming
  • Extra Credit
    • MVC
    • Dependency Injection
    • Security Guide
Powered by GitBook

Support Us

  • Become a Patreon
  • Contribute
  • CFCasts

Social Media

  • LinkedIn
  • Facebook
  • Twitter

Ortus Solutions, Corp

On this page
  • Try/Catch/Finally
  • Catch Types
  • Native Exception Types
  • Custom Exception Types
  • Throwing Exceptions
  • Rethrowing Exceptions

Was this helpful?

Edit on GitHub
  1. CFML Language

Exception Management

PreviousConditionalsNextComponents

Last updated 1 year ago

Was this helpful?

Try/Catch/Finally

The CFML language also provides you with a traditional approach to deal with error handling at the code block level. This is usually a trio of constructs:

  • try: The try block allows you to demarcate the code to test if it fails or passes ()

  • catch : The catch block is executed when the try block fails ()

  • finally : The finally block executes no matter if the try fails or passes. It is guaranteed to always execute. ()

Basically, a try and catch statement attempts some code. If the code fails, CFML will do whatever is in the exception to try to handle it without breaking. Of course, many different types of exceptions can occur, which should sometimes be handled in a different manner than the others.

try{
    // code to try to execute
} catch( any e ) {
    // the any type catches ALL errors from the try above
} catch( myType e ){
    // Catch the `myType` only type of exception
} finally {
    // this code executes no matter what
}

Catch Types

The catch construct can take an any or a custom exception type declared by the CFML engine, Java code or custom exceptions within your code. This is a great way to be able to intercept for specific exception types and address them differently.

try{

} catch( database e ){

} catch( template e ){

}

Native Exception Types

Some of the exception types found in CFML are the following

  • application: catches application exceptions

  • database: catches database exceptions

  • template: catches ColdFusion page exceptions

  • security: catches security exceptions

  • object: catches object exceptions

  • missingInclude: catches missing include file exceptions

  • expression: catches expression exceptions

  • lock: catches lock exceptions

  • java.lang.Exception: catches Java object exceptions

  • searchengine: catches Verity search engine exceptions

  • any: catches all exception types

Custom Exception Types

Custom exception types are defined by you the programmer and they can also be intercepted via their defined name. Let's say that the exception type is "InvalidInteger" then you can listen to it like this:

try{
    throw( type="invalidInteger" );
} catch ( "InvalidInteger" e ){

}

Throwing Exceptions

The throw() function or tag has several attributes:

  • Type : A custom or CFML core type

  • Message : Describes the exception event

  • Detail : A detailed description of the event

  • errorCode : A custom error code

  • extendedInfo : Custom extended information to send in the exception, can be anything

  • object : Mutually exclusive with the other attributes, usually another exception object or a raw Java exception type.

try {
    throw( message="Oops", detail="xyz", errorCode=12 );
} catch (any e) {
    writeOutput( "Error: " & e.message);
} finally {
    writeOutput( "I run even if no error" );
}

Rethrowing Exceptions

try{
	runAroundEachClosures( arguments.suite, arguments.spec );
} catch( any e ){
	rethrow;
} finally {
	runAfterEachClosures( arguments.suite, arguments.spec );
}

// Mix In Stub
try{
	// include it
	arguments.targetObject.$include = variables.$include;
	arguments.targetObject.$include( instance.mockBox.getGenerationPath() & tmpFile );
	structDelete( arguments.targetObject, "$include" );
	// Remove Stub
	removeStub( genPath & tmpFile );
} catch( any e ) {
	// Remove Stub
	removeStub( genPath & tmpFile);
	rethrow;
}

custom_type: catches the specified custom exception type that is defined in a tag

Now that you have seen how to listen to exceptions, let's discover the throw or cfthrow constructs used to throw a developer-specific exception. ()

The rethrow or cfrethrow construct allows you to well, rethrow the active exception by preserving all of the exception information and types. Usually you use rethrow within a catch block after you have done some type of operations on the incoming exception. ()

https://cfdocs.org/cftry
https://cfdocs.org/cfcatch
https://cfdocs.org/cffinally
cfthrow
https://cfdocs.org/cfthrow
https://cfdocs.org/cfrethrow