Bugs, Errors, and Exceptions

It is important to distinguish exceptions from bugs and errors. A bug is a programmer mistake that should be fixed before the code is made available to users. An exception is usually not the result of a programmer mistake (though such mistakes can also raise exceptions). Rather, exceptions are raised as a result of problems that you can predict, but can’t prevent, because they depend on factors outside your program. A network connection dropping suddenly, or running out of disk space, are both examples of exceptions that you can handle.

An error is caused by user action. For example, the user might enter a number where a letter is expected. Once again, an error might cause an exception, but you can prevent that by implementing code to validate user input. Whenever possible, you should anticipate user errors and write code to prevent them, instead of relying on exceptions.

Even if you remove all bugs and anticipate all user errors, you will still run into predictable but unpreventable problems, such as running out of memory or attempting to open a file that no longer exists. These are exceptions. You cannot prevent exceptions, but you can handle them so that they do not bring down your program.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset