Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use java exceptions knowledge to solve this problem. code below: QUESTION 5 Two

ID: 3689799 • Letter: U

Question

Use java exceptions knowledge to solve this problem.

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: ) me- scanner.next): String name scanner.next0; if (name.length throw new RuntimeException("User name must include at least one character."); least one character.") return name; public static String getUserNameBO Scanner scanner new Scanner(System.in); System.out.println( Enter your user name: " String name scanner.next0: if (name.length0 0) throw new Exception("User name must include at least one character."); return name; getUserNameA O getUserNameB

Explanation / 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.