Google

Saturday, January 31, 2009

SCJP - Creating your own Exceptions

Exercise page378

import java.lang.Exception;

public class MyException {

public static void main(String[] args) {
MyException ex = new MyException();
try {
ex.checkFood("Pizza");
} catch (BadFoodException bfe) {
System.out.println(bfe.getMyError());
}
}

private void checkFood(String food) throws BadFoodException {
if (food.equals("Pizza")) {
throw new BadFoodException();
}
}
}

class BadFoodException extends Exception {
public String getMyError(){
return "I don't like this food";
}
}