O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Handling a Built-in Exception

At the very beginning of this chapter, we learned that we can respond to both built-in and custom errors using the try/catch/finally statement. So far, the errors we've handled have all been custom errors. To close this chapter, let's examine a try/catch/finally statement that handles a built-in error.

Suppose we're building a chat application in which the user is asked to specify a port number when connecting to the chat server. We assign the specified port number to a variable named userPort. Then, we use the Socket class to attempt to connect to the specified port. In some cases, the connection will fail due to security limitations. To indicate that a security limitation has been breached when a connection attempt is made, the Flash runtime throws a SecurityError. Therefore, when attempting to make a connection, we wrap the connection code in the try block. If the connection fails due to security reasons, we display an error message to the user indicating what went wrong.

var socket:Socket = new Socket();
try {
  // Attempt to connect to the specified port
  socket.connect("example.com", userPort);
} catch (e:SecurityError) {
  // Code here displays message to the user
}

Tip

For a list of circumstances that can cause socket connection failures, see the Socket class's connect( ) method in Adobe's ActionScript Language Reference.

Error Events for Problem Conditions

In the preceding section, we saw how to handle an exception caused by an illegal socket-connection ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required