Exception Management

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:
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.
1
try{
2
// code to try to execute
3
} catch( any e ) {
4
// the any type catches ALL errors from the try above
5
} catch( myType e ){
6
// Catch the `myType` only type of exception
7
} finally {
8
// this code executes no matter what
9
}
Copied!

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.
1
try{
2
3
} catch( database e ){
4
5
} catch( template e ){
6
7
}
Copied!

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
    custom_type: catches the specified custom exception type that is defined in a cfthrow tag
    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:
1
try{
2
throw( type="invalidInteger" );
3
} catch ( "InvalidInteger" e ){
4
5
}
Copied!

Throwing Exceptions

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. (https://cfdocs.org/cfthrow)
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.
1
try {
2
throw( message="Oops", detail="xyz", errorCode=12 );
3
} catch (any e) {
4
writeOutput( "Error: " & e.message);
5
} finally {
6
writeOutput( "I run even if no error" );
7
}
Copied!

Rethrowing Exceptions

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/cfrethrow)
1
try{
2
runAroundEachClosures( arguments.suite, arguments.spec );
3
} catch( any e ){
4
rethrow;
5
} finally {
6
runAfterEachClosures( arguments.suite, arguments.spec );
7
}
8
9
// Mix In Stub
10
try{
11
// include it
12
arguments.targetObject.$include = variables.$include;
13
arguments.targetObject.$include( instance.mockBox.getGenerationPath() & tmpFile );
14
structDelete( arguments.targetObject, "$include" );
15
// Remove Stub
16
removeStub( genPath & tmpFile );
17
} catch( any e ) {
18
// Remove Stub
19
removeStub( genPath & tmpFile);
20
rethrow;
21
}
Copied!
Last modified 2yr ago