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

Two static methods are listed below. Which of them needs a \"throws\" clause in

ID: 3931995 • Letter: T

Question

Two static methods are listed below. Which of them needs a "throws" clause in order to compile? public static String getUserNameA() {Scanner scanner = new Scanner(System.in); System.out.printlnfEnter your user name:"); String name = scanner.next(); if (name.length() ==0){throw new RuntimeExceptionf User name must include at least one character.");} return name; public static String getUserNameB() {Scanner scanner = new Scanner(System.in); System.out.printlnfEnter your user name:"); String name = scanner.next(); if (name.length() ==0) {throw new ExceptionfUser name must include at least one character.");} return name;} getUserNameA getUserNameB

Explanation / Answer

Answer: getUserNameB

getUserNameB() method needs a throws cluase to throw an exception because Exception class is checked Exception that can be caught by compiler. In getUserNameB() method, if name length is 0 then we are throwing Exception class so it needs throws clause because it is an checked exception.

Where as getUserNameA() method throws RuntimeException which is Unchecked exception so it can be caught by compiler.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote