Today, I found that cfthrow is very useful to me for error handling in my project. Because prior programmer of my current wrote some funny code for error handling without using cfthrow. I'm not sure whether they don't know cfthrow or not. Honestly, I didn't know most of Coldfusion Tags and functions before I took CF8 exam. When preparing for CF8 exam, I know most of tags and functions of CF8. Thanks, CF8 exam.

Ok, let's continue. Here is funny coding our prior programmer wrote.

view plain print about
1<cftransaction action="BEGIN">
2    <cftry>
3        [CFQuery]
4        .....
5        .....
6        [/CFQuery]    
7        <cfif chkDeath.recordcount NEQ 0>
8            REGISTRATION IS NOT ALLOWED FOR A DECEASED PATIENT.
9            <cfabort>
10        <cfelseif chkDoubleReg.recordcount NEQ 0>
11            DOUBLE REG IS NOT ALLOWED.
12            <cfabort>    
13        </cfif>    
14        .....
15        .....
16        [other transactions]
17        .....
18        <cftransaction action="COMMIT">
19        <cfcatch type="Any">
20            <cftransaction action="ROLLBACK">
21            <cfoutput>#cfcatch.message#</cfoutput>
22            <cfabort>
23        </cfcatch>
24    </cftry>
25</cftransaction>

You will see above coding is so funny and not programmatically structure at all. Because he/she used another way of error handling instead of using cfthrow. That's why I need to add cfthrow in cfif/cfelse of above coding. I didn't say my changes coding is perfect but I'm sure it's more efficient than prior one.

view plain print about
1<cfif chkDeath.recordcount NEQ 0>                    
2        <cfthrow
3            type="DeceReg"
4            errorcode="DECReg"
5            message="REGISTRATION IS NOT ALLOWED FOR A DECEASED PATIENT.">

6    <cfelseif chkDoubleReg.recordcount NEQ 0>                                                                    
7        <cfthrow
8            type="DoubleReg"
9            errorcode="DBReg"
10            message="DOUBLE REG IS NOT ALLOWED.">

11    </cfif>