Q:

how to throw an exception in java

I have a loop running for number of records and upon a specific condition, I need to add an errorCode and errorMessage to the output response as below
addErrorMessage(Constants.ZERO_AMPU, msg, infoRow) where the method sets the error code and message.
I have defined user defined exception to throw exception on the same condition so that it does not loop through all the records 
if (ampu.intValue() == 0){

            def message = "ampu value is zero"

            Constants.addErrorMessage(Constants.ZERO_AMPU, message, tariffOffer)
            Ampu a = new Ampu();
            throw a;
}

class Ampu extends Exception{
        public Ampu(){}
            def message = "ampu value is zero"
            Constants.addErrorMessage(Constants.ZERO_AMPU, message, infoRow)
            return "ampu value is zero";

    } 

what am I missing in throwing the exception here ?

A:

Remove the return value:
public Ampu(){}
        message = "ampu value is zero"
        Constants.addErrorMessage(Constants.ZERO_AMPU, message, infoRow)
} 

Since an exception is stil an object it should have a valid constructor.
message should be defined as a string(but it's already  a field so don't declare it).

