Two static methods are listed below Which of them needs a th
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
Solution
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.
