Exceptions |
[Only cover first 4 sections of this chapter now. Other parts will come when we talk about streams.]
We have seen exceptions all semester long:
These are just a few examples of a more general system provided by Java (and some other programming languages) to deal with errors called exceptions.
We will look at what exceptions can do for us and how Java implements them using variants of this Color Mixer program covered earlier:
Demo: Color MixerIn this simple version, things work fine, until we enter non-numerical text. When this is passed to Integer.parseInt, a NumberFormatException is thrown.
try { bunches of statements } catch { bunches of statements }The idea is that the "bunch of statements" in try might throw an exception, and if it does, some code in the "bunch of statements" in catch can respond to it.
Exceptions |