0

what are the examples of self defined exceptions?

java relatex

20th Aug 2016, 8:28 PM
Musiimenta Joanitah
Musiimenta Joanitah - avatar
2 Respuestas
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
En tendencia hoy
.
1 Votes
What?
0 Votes
HTML
0 Votes
Web
0 Votes
Quiz duel
0 Votes
FRC Coding?
1 Votes
help
0 Votes
AI
2 Votes
APIs
1 Votes