Use java Exceptions knowledge to solve this question. Code below: QUESTION 5 Two
ID: 3689800 • Letter: U
Question
Use java Exceptions knowledge to solve this question.
Code below:
QUESTION 5 Two static methods are listed below. Which of them needs a "throws" clause in order to compile? public static String getUserNameA0 Scanner scanner new Scanner(System.in); System.out.println( Enter your user name: " String name scanner.next0 if (name.length0) throw new RuntimeException("User name must include at least one character."); one character" return name public static String getUserNameB0 Scanner scanner new Scanner(System.in) System.out.println( Enter your user name: "; String name scanner.next0: if (name.length0 ) throw new Exception(User name must include at least one character."); return name; O getUserNameA O getUserNameBExplanation / Answer
Answer: getUserNameB needs throws clause.
In Java, you can throw anything that extends the Throwable class. However, you don't need to specify a throws clause for all classes. Specifically, classes that are either an Error or RuntimeException or any of the subclasses of these two. In your case Exception is not a subclass of an Error or RuntimeException. So, it is a checked exception and must be specified in the throws clause, if you don't handle that particular exception. That is why you needed the throwsclause.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.