Hey, Can i know how to do the throw and catch for my java code ? i need to create a OfferException class which allow a suitable error message to be specified when there is an error.

this is my code..

public boolean makeOffer(int offerPrice)

          if (!super.getSaleStatus())
          {
          System.out.println("Error - Property Sale is closed!");
          return false;
          }
          else if(offerPrice <= super.getCurrentOffer())
          {
          System.out.println("Error - New offer must be higher than current offer!");
          return false;
          }
          else
          {
          super.setCurrentOffer(offerPrice);
          //do something
          return true;
          }
          }

Here is example how to create own exception

class OfferException extends Exception {
    OfferException() {
    }

    OfferException(String message) {
        super(message);
    }

    OfferException(String message, Throwable cause) {
        super(message, cause);
    }
}

then somewhere in code

private static int latestOffers(String someString) throws OfferException {
        try {
            //do something
        } catch (OfferException e) {
            throw new OfferException("Did not found any offers");
        }
    }

Edited 3 Years Ago by peter_budo: Fixing code

This article has been dead for over six months. Start a new discussion instead.