OO Programming with Java


Error handling

  1. Exception being defined as classes, they can have attributes/operations:
public class UnavailableException extends Exception { 
  private int limit;
  public UnavailableException(int limit) 
  { this.limit = limit; }
  public int getLimit() 
  { return limit; }
}

That can be used with the throw statement:

throw new UnavailableException(book.getQuantity());

And the try_catch:

try {
  bookstore.buy("Core Java", 30);
} catch (UnavailableException e) {
  System.out.println("Avalaible quantity: "+e.getLimit());
}

4 - 12