Handling Errors with try/catch
Server-Side ActionScript contains the try/catch/finally construct of ECMAScript. If you have used JavaScript, Java, or ColdFusion before, you may be familiar with this construct, which is missing from client-side ActionScript. You use it like this:
try { // Code here } catch(exception) { // Error handling code here } finally { // Do this in either case }
Tip
A try/catch/finally construct says, “Try to execute the code inside the try block. If there is an error (exception), execute the code in the catch block. In either case, execute the code in the finally block.”
To demonstrate, look at this SSAS code:
function getProducts ( ) { var sql = "SELECT ProductID, ProductName FROM Products"; try { var myResults = CF.query("northwind", sql); } catch (e) { sendEmailAdmin(e); throw("There was an error connecting to the database"); } return myResults; }
In this case, the query to the database is wrapped in a
try/catch block. This
allows us to capture any error when connecting to the database and
perform some additional steps. In this case, we’ve
called an imaginary function called sendEmailAdmin(
) that resides in the same file, allowing us to send a
notification email to the administrator that an error occurred. After
sending the email, we create our own error using the
throw
keyword. When we throw an error, we are in control of what is sent to the Flash movie. We can use this to send a code or an error message of our own rather than a system error message. When ...
Get Flash Remoting: The Definitive Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.