Accessibility

Table of Contents

User experience considerations with SQLite operations

Handling errors

Up to this point the code assumes that the SQLConnection gets established without errors. Alas, this isn't always the case. To make sure an application does the right thing we need to look at error handling. The execution mode being used will dictate how to detect that an error occurred. If explicit error handling is not specified, any errors that are generated will be handled silently, unless the application is running in a debug environment.

For a synchronous connection, any database operation that might cause an error should be surrounded with a try..catch block. Multiple database operations can be combined into the same try..catch block if an error generated by any of those operations should be handled the same way. Returning to the example of opening a synchronous connection, the following example introduces the needed error handling.

try {
    sqlConnection.open(null, SQLMode.CREATE);
} catch(sqlError:SQLError) {
    // Handle error establishing connection here.
}

An asynchronous connection opened using event listeners will dispatch an event of type SQLErrorEvent.ERROR to indicate that a problem occurred:

sqlConnection.addEventListener(SQLEvent.OPEN, handleConnectionOpen);
sqlConnection.addEventListener(SQLErrorEvent.ERROR, handleConnectionError);
sqlConnection.openAsync(null, SQLMode.CREATE);

Lastly, in the case of using a Responder, the second argument that is passed into the Responder constructor indicates the function to call if the database operation fails:

sqlConnection.openAsync(null, SQLMode.CREATE, new Responder(handleConnectionOpen, handleConnectionError));

In both the event listener and Responder cases the function used to handle the error condition must declare that it takes a single parameter of type SQLErrorEvent. The definition of the handleConnectionError() function used above would have the following form:

function handleConnectionError(sqlErrorEvent:SQLErrorEvent) {
    // Handle error establishing connection here.
}

As mentioned in the previous section, if an asynchronous connection is given both an event listener and a Responder instance, the Responder functions will take precedence and be called instead of the registered event listener. If a Responder is used but the second constructor argument is not supplied, any errors will be handled silently regardless of whether a debug environment is used.