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;
          }
          }
if ( actionIsInvalid )
  throw new Exception("tadaaaa!!!");

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");
        }
    }

Peter, I would pass 'message' as parameter to the exception :)

commented: Done +15

... and throw an OfferException rather than a DivideByZeroException
ahh... the power of copy/paste...
;)

commented: :) +14

Yes in deed power of copy and paste. Fixed

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.