Validate user input and directory specifications:
- The two most common causes of run-time failures are incorrect user input and invalid directory specifications.
-
Designing for user input:
- Structure required input in terms the user will find easy to understand, e.g., labeled and consistent with the application's metaphor.
- Parse the input for errors and request re-entry if invalid, explaining carefully how to supply correct input. If your design of user input is hard to parse and validate then you need to re-think and revise the design. Often use of regular expression matching makes validation relatively painless. You will find a regular expression parser in the Boost libraries - www.boost.org.
-
Directory specifications and file handling are another frequent source of run-time errors:
- A directory path may not exist on the machine running the application.
- A file may not exist or be misidentified or be opened exclusively by another stream.
- File reads may have reached the end of the file.
Conclusions:
Programs are robust, not because they never encounter errors, but because they implement processing for validation and error handling.
- Always validate user input and provide help for the user to correctly supply input.
- Validate directory and file operations by wrapping stream operations in a try block. Consider rethrowing meaningful exception messages when errors occur.