NOTIFICATION: These examples are provided for educational purposes. Using this code is under your own responsibility and risk. The code is given ‘as is’. I do not take responsibilities of how they are used.
Errors are unforseen. They are a result produced by coding and they are unplanned and/or unanticipated by the developer.
However, we can manage these errors and used them to our advantage.
Errors are handle by the server is the following fashion:
- The first level are the system errors.
System error are generated when some components are not working properly.
For example, if your application requires access to a database and this database is at a server which is down, then this could generate your application to fail.
- The second level are the code level errors. These errors (or exceptions) include logic, syntax and other type of errors that we will see later
- The third level of errors are at the application level
- The last level of errors are at the server level.
Error Flow Diagram:
As you may notice from the figure 1, we have a classification for all the errors that can be generated:
- Logic: These kind of errors are generated from some fault in the design of the code.
For example, you could have an infinite loop:
<CFSET counter = 0> <CFLOOP CONDITION='counter GTE 10'> <CFOUTPUT> Counter: #counter# <BR/> </CFOUTPUT> </CFLOOP>
This loop will work while the counter is not greater or equal (GTE) to 10.
The problem is that there is no part of this code that increases the counter.
Since the variable counter is always 0, the loop is considerate infinite and can affect the server
- Syntax: These errors are related with misspelling, invalid data types, invalid parameters, and everything related with the language syntax. This kind of errors are caught on the application server and server level.
<CFOUTPUT QUERY='yourQuery'> <CFOUTPUT>Count of Records: #yourQuery.recordCount# <BR/></CFOUTPUT> <CFOUTPUT>#yourQuery.nonExistentField# <BR/></CFOUTPUT> </CFOUTPUT>
The second like is going to work; however, the third line is asking for a field that doesn’t exist in myQuery structure.
<CFOUTPUT QUERY='yourQuery'> <CFOUTPUT>Count of Records: #yourQuery.recordCount# <BR/><CFOUTPUT> <CFOUTPUT>
Here you are forgetting to close both CFOUTPUTs.
- Runtime: These are unforseen conditions such as data type mismatches, out of scope issues, server-side form validation errors, etc.
<CFPARAM NAME='user_age' default='My name is Alejandro'> <CFPARAM NAME='legal_driving_age' default=21> <CFIF Int(user_age) GT legal_driving_age> <CFOUTPUT>This user can apply for a regular driving licence</CFOUTPUT> <CFELSE> <CFOUTPUT>This user cannot apply for a regular driving licence.</CFOUTPUT> </CFIF>
This example fail on: <CFIF Int(user_age) GT legal_driving_age>
Because user_age is a string without numbers while the function Int() is expecting a string with number.
Since Int() cannot convert the string, a runtime error will be generated.
There are three way to caught these exceptions which we would see later:
- Via code-level
- Via application-level
- Via server-level
- Validation: We can see validation errors as of the runtime errors that we are going to have to deal with.
This kind of erros happens when Coldfusion server-side form catches a problem with the submitted data.
<FORM ACTION='process.cfm' METHOD='POST'> <INPUT TYPE='text' NAME='age_integer' value='only text, no integers here'> <INPUT TYPE='submit' value='Submit' /> </FORM>
You may ask: where is the error here?
Well, in coldfusion we can do integer validation of the input by adding “_integer” to the name of the input field.
In this case, “age_integer” means that we have a input name “age” which should be validated for integers.
Since the value is “only text, no integers here” which is pure string, this will fail the validation.
For more information you can go to the following link:
Lets clarify that these kind of errors can only be caught via error handlers at the aplication-level.
- System: Errors related with the system are always about inaccessible databases, unavailable resources, incorrect or invalidly configured servers, and file system errors such as lack of permissions.
These errors can be caught:
- Via code-level handlers
- Via applicatoin-level handlers
- Via server-level handlers
- Request: These errors occurs when the client request for invalid resources not available from the server side.
Example:Such errors can be caught via server-wide error handlers.