what are the examples of self defined exceptions? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

what are the examples of self defined exceptions?

java relatex

20th Aug 2016, 8:28 PM
Musiimenta Joanitah
Musiimenta Joanitah - avatar
2 Answers
0
java
20th Aug 2016, 8:29 PM
Musiimenta Joanitah
Musiimenta Joanitah - avatar
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()); } } }
21st Aug 2016, 7:45 AM
Tiger
Tiger - avatar