Поделиться через


Error Handling and Debugging

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

All code contains errors of one kind or another and how you deal with errors may be the most important part of a well-designed application. There are two categories of errors: those you can prevent, which are called development errors, and those you can't prevent but can trap, which are called run-time errors.

Development errors are either syntax errors or logic errors. Syntax errors occur from typographical errors, missing punctuation, or improper use of a language element; for example, forgetting to properly terminate an If…Then…Else statement. Logic errors are more commonly referred to as "bugs." These errors occur when code executes without causing an error, but does not produce the results intended. You eliminate development errors by "debugging" your code. There are a wide variety of tools that can help you debug script and Visual Basic for Applications (VBA) code.

Run-time errors are errors that occur while the application is running. These are errors where otherwise correct code fails due to invalid data or system conditions that prevent the code from executing (for example, lack of available memory or disk space). You handle run-time errors by writing error handlers and by writing procedures that can validate program or environmental conditions in appropriate circumstances.

Successfully debugging code is more of an art than a science. The best results come from writing understandable and maintainable code and using the available debugging tools. When it comes to successful debugging, there is no substitute for patience, diligence, and a willingness to test relentlessly, using all the tools at your disposal.

Writing good error handlers is a matter of anticipating problems or conditions that are beyond your immediate control and that will prevent your code from executing correctly at run time. Writing a good error handler should be an integral part of the planning and design of a good procedure. It requires a thorough understanding of how the procedure works and how the procedure fits into the overall application. And, of course, writing good procedures is an essential part of building solid Microsoft Office solutions.

Contents

Writing Error-Free Code

Debugging Code

Handling Errors

Where to Go from Here