0
what are the examples of self defined exceptions?
java relatex
2 Answers
0
java
0
see the below:
class WrongAnswer extends Exception {
public WrongAnswer(String message){
super(message);
}
}
class Question{
public void isCorrect(String answer) throws WrongAnswer {
if (answer == "bird"){
System.out.println("Correct!");
}else {
throw new WrongAnswer(answer + " is a wrong answer");
}
}
}
public class Program
{
public static void main(String[] args) {
Question q = new Question();
try{
q.isCorrect("Monkey");
} catch(WrongAnswer wa){
System.out.println(wa.getMessage());
}
}
}