9.4. Summary

This program gave you information that enables you to intelligently anticipate and handle errors in your programs. You learned the following:

  • You can use the on error clause in a try block to catch errors that would otherwise cause your program to terminate.

  • You can use the error command to display a system error message or your own custom message before your program is terminated.

  • You can catch the error number generated by a particular error using the on error command and then insert code in your program to handle that particular error.

Handling errors intelligently and cleaning up before your program terminates is part of good programming discipline. It's always a good strategy to run your program and intentionally try to create errors to see what can go wrong. If you're writing a script that others may use, it's particularly important that you bullet-proof your programs so that they terminate as gracefully as possible when an error occurs. I show you many more examples of error handling throughout the rest of this book.

In the next chapter, you learn how to talk to applications. Arguably, this is the single most important thing you need to learn to become an effective AppleScript programmer. Before proceeding, however, try the exercises that follow to test your understanding of the material covered in this chapter. You can find the solutions to these exercises in Appendix A.

Get Beginning AppleScript® 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.