package lectures.exceptions.extra;
import util.annotations.WebDocuments;
@WebDocuments({"Lectures/ExceptionsExtra.pptx", "Lectures/ExceptionsExtra.pdf", "Videos/ExceptionsExtra.avi"})
public class AWeightSetterWithFinallyAndAssertions implements WeightSetterWithFinallyAndAssertions {
double weight;
public boolean preSetWeight(double newValue) {
return newValue > 0;
}
public boolean preSetWeight() {
return preSetWeight(weight);
}
public void setWeight(double newValue) {
assert preSetWeight(); try {
if (!preSetWeight(newValue)) {
System.out.println("Ignoring illegal weight");
return;
}
weight = newValue;
System.out.println("Assigning legal weight");
} finally {
System.out.println("Asserting precondition/assertion");
assert preSetWeight(); }
}
public static void main (String[] args) {
WeightSetterWithFinallyAndAssertions aWeightSetter = new AWeightSetterWithFinallyAndAssertions();
aWeightSetter.setWeight(1);
aWeightSetter.setWeight(-1);
}
}