Chapter 13. Exceptions and Error Handling

In this chapter, we’ll explore ActionScript’s system for generating and responding to runtime errors—or exceptions. In ActionScript, errors can be generated both by the Flash runtime and by the program that is executing. Errors generated by the Flash runtime are known as built-in errors; errors generated by a program are known as custom errors. In a program, we can respond to, or handle, any error (whether built-in or custom) using the try/catch/finally statement; we can generate an error via the throw statement.

To learn how to generate and respond to errors in a program, we’ll revisit our virtual zoo program.

Tip

This chapter’s updates to the virtual zoo program are the last we’ll make until the end of this book. To complete the virtual zoo program we must discuss display programming and mouse input, both of which are covered in Part II. After you read Part II, consult the "Appendix A,” (Appendix) to see how to add graphics and interactivity to the virtual zoo program.

The Exception-Handling Cycle

Recall that the VirtualPet class defines a setName( ) method that sets the petName variable of VirtualPet instances. As a refresher, here’s the relevant VirtualPet class code (portions of the class that do not pertain to setting the petName variable are omitted):

public class VirtualPet { private var petName:String; public function setName (newName:String):void { // If the proposed new name has more than maxNameLength characters... if (newName.length ...

Get Essential ActionScript 3.0 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.