N342 Error Handling |
Modified:
|
Designers of Web-based systems practice in a dangerous environment where their systems are routinely exposed to naive users and professional attackers.
Errors committed by users, such as not completing a form, should be handled by the user interface on the client-side.
Syntax errors are violations of language rules, simple to discover and correct before the program will execute.
Runtime or execution time errors can occur for unpredictable reasons and times: the database server lost power, an SQL statement was incorrect, etc. Systems cannot be fully tested so that runtime failures are always possible.
Studies analyzing the cause of downtime to online systems found that over 50% of the episodes were due to human operators, not programming errors or user errors. Even well designed and tested systems will fail, as software designers and implementers we should handle failures as gracefully as possible, minimizing the consequences, and providing the user with information that a failure occurred and their options.
There are several points to consider about system faults:
Most modern languages handle detected failures generally as exceptions where an exception is some out of the ordinary condition that results in the execution of code reserved for dealing with the exceptional condition. Many languages implement exception handling with the try-catch-finally statement discussed below.
Portions of the following description was extracted from the Microsoft Visual Studio Help
- try Statements
- Required. Statements where an error can occur.
- exception
- Required. Any variable name. The initial value of exception is the value of the thrown error.
- catch Statements
- Optional. Statements to handle errors occurring in the associated tryStatements.
- finally Statements
- Optional. Statements that are unconditionally executed after all other error processing has occurred.
The try...catch…finally statement provides a way to handle some or all of the possible errors that may occur in a given block of code, while still running code. If errors occur that the programmer has not handled, JScript simply provides its normal error message to a user, as if there was no error handling.
- try contain code where an exception may be thrown. If an exception is thrown while executing in the try, program control is passed immediately to catch.
- catch contains the code to handle any exception thrown, if no error occurs, catch is never executed.
- finally contains code that is always executed after the try or catch is finished.
- throw raises an exception, within a try, program execution transfers to the matching catch.
If the error cannot be handled in the catch associated with the try where the error occurred, use the throw statement to propagate, or rethrow, the error to a higher-level error handler.
After all statements in try have been executed and any error handling has occurred in catch, the statements in finally are unconditionally executed.
Notice that the code inside finally is executed even if a return statement occurs inside the try or catch blocks, or if the catch block re-throws the error. finally is guaranteed to always run, unless an unhandled error occurs (for example, causing a run-time error inside the catch block).
Example 0
| print("1"); try { print("2"); print("3"); } catch(e) { print("4 " + e); } finally { print("5"); } function print( s ){ document.write( s ); } |
Produces the following output:1 2 3 5
|
Example 1
| print("1"); try { print("2"); throw "an exception"; print("3"); } catch(e) { print("4 " + e); } finally { print("5"); } function print( s ){ document.write( s ); } |
Produces the following output:1 2 4 an exception 5
|
The following example illustrates JavaScript exception handling in a nested hierarchy. The general rules are:
- A throw inside a try will execute the companion catch.
- A throw inside a catch or finally will execute an enclosing catch.
| try { // throw "first exception"; print("1"); try { |
Produces the following output:1 2 4 an exception 2 6 7 an exception 2 re-thrown 8
|
Exercise 1What is the output after un-commenting the line in Example 2?
|
The following example is from the project, Portfolio.asp. The essential approach is that any reported exception will be caught and somewhat gracefully reported to the user.
By wrapping nearly the complete, normal execution script in a try block, all exceptions will be treated identically.
|
<%@ Language=JScript%> |
Stock Trader - Technical ErrorTrading cannot be continued at this time. Please try again later. Contact 1-800-123-4567 to report this problem
|
Exercise 2
|
Rather than simply displaying an error message and depending upon the user to report the error, more useful for the developer is to try individual elements that throw exceptions and write details to a system log for later analysis.
The following example has separate try/catch statements for each source of an exception (i.e. the operations that throw exceptions to the caller).
By handling each exception, specific error information can be logged to a server file for later analysis. Here, we present the user with the same error message in any failure case.
|
<%@ Language=JScript%>
} catch(e) { }
<% } %> |
Stock Trader - Technical ErrorTrading cannot be continued at this time. Please try again later. Contact 1-800-123-4567 to speak to Error in XSL of PortfolioContent.xsl Response.Write(xmlDoc.transformNode(xsl));
Produces contents in errorlog.txt of: Thu Jun 18 05:32:50 EDT 2012 |
Note
The script also handles exceptions when attempting to write the error log by executing within a try/catch statement.
A more complete solution:
- log the error,
- empty the trader's cart,
- logout the trader,
- abandon the session,
- create a separate, error notification file and redirect to that file.
IUS Network file logging
Error logs (and other files) can be written to locations other than the server executing the program.
The following opens and defines a networked file on the IUS W: drive.
a = fs.OpenTextFile("\\\\se-cser-nas1\\homepages\\ads\\username\\N342\\Project\\errorlog.txt", ForAppending, true);
If using a locally administered IIS, to write to some other location than that of the program, change to:
a = fs.OpenTextFile("C:\\N342\\Project\\errorlog.txt", ForAppending, true);
Exercise 3
|