Write an exception inside of a loop, then outside of a loop when live-coding so that students learn the effects of placement on exceptions.

  • Show students that they should put the exception inside the loop if they want the program to abort, and put it outside the loop if they want the program to continue.
  • Check out our example Java code that demonstrates error checking with three different methods that sum numbers in a list.
    • The first way is to forgo error checking altogether.
    • The second way is to have code continue upon encountering an error.
    • The third way is to have code halt upon encountering an error.
  • To help students solidify this concept, give them an assignment in which they must do the following:
    • Write any exception that works (using try/catch syntax).
    • Put this exception in a larger context and then show how we have exception propagation up to the caller.
    • Use an exception within a loop!

More about this tip

External Source
Interview with Cyndi Rader.