Errors and Exceptions
- 9.6k Downloads
Until this point we have used assert statements to ensure that a condition required by the code to generate correct output is met. For example, we might check that a number that we were going to take the square root of is non-negative, and trip an assertion if this condition were not met. Whilst this approach is very useful as it pinpoints errors, it is rather inflexible as it terminates the code at the instant that an assertion is tripped. We would much rather the code instead made some attempt to fix the problem itself, if that were possible, warned us of the possible effects of the fix if necessary, and then carried on executing. This is possible through the use of exceptions, which are the focus of this chapter.