CSC152 2005F, Class 26: Review of Exceptions Admin * Continuing strategy of last week * Data collection: Second year retreat Overview * Parsing, continued Parsing, Continued * Two concerns! * What happens when someone writes a number too large? * What happens when we hit a non-digit? * As a programmer, you need to decide what to do when you encounter one of these kinds of errors. * Preparation: Think about when these errors might occur. * Reaction: * "Pass the buck"; Throw an exception * "Pass the buck"; Return a special value * Skip the erroneous stuff (ignore non-digits; stop before overflow) * Stop as soon as you hit an error and assume that the erroneous stuff belongs to another part of the program * Print an error message (NO!) * Return a different kind of value (or restart the process in a different way). (HARD) * Set a precondition and let the caller suffer if the precondition is not met. * As a Java programmer, throwing exceptions is likely to be your default behavior. * Permits the *caller* to decide what is best to do. try { int digit = charToInt(str.charAt(pos)); result = result + digit * place; place = place*10; } catch (Exception e) { // Skip offending character. }