previous | start | next

## Exception Handling

- One common use of exception handling is in functions that read input.
- Example: suppose that no price or score is given?

  >     bool Product::read(fstream& fs)
  >     {
  >        getline(fs, name);
  >        if (name == "") return false; // end of file
  >        fs >> price >> score;
  >        if (fs.fail())
  >           throw runtime_error("Error while reading product");
  >        string remainder;
  >        getline(fs, remainder);
  >        return true;
  >     }

- When a logic error occurs, it never makes sense to retry the same operation, but a runtime error may have some chance of going away when the operation is attempted again.
- Note that this function distinguishes between the expected end of file (returning false), and an unexpected problem (which throws an exception).

previous | start | next