4 Exceptions
A procedural abstraction is a mapping from arguments to results, with possible modification of some of the arguments. The arguments are members of the domain of the procedure, and the results are members of its range.
A procedure often makes sense only for arguments in a subset of its domain. For example, a procedure that computes the factorial makes sense only if its argument is positive. As another example, the search
procedure can return the index of the element only if the element appears in the array.
One way of coping with such a situation is to use partial procedures, as discussed in Chapter 3. For example, we might define gcd
only when its arguments are positive:
public static int gcd (int n, int d) // REQUIRES: n, d > 0 ...
Get Program Development in Java: Abstraction, Specification, and Object-Oriented Design now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.