LearnFrenzy
Sign in Take a free test

Generics · Find the Error · Question 9

Error: a generic exception class Answer, explanation & discussion

  • Find the error
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 9 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

Main.java
class ValidationException<T> extends Exception {
    private static final long serialVersionUID = 1L;
    final T badValue;
    ValidationException(T badValue) { this.badValue = badValue; }
}

public class Main {
    public static void main(String[] args) {
        System.out.println("validate");
    }
}
  • A badValue must be private
  • B A generic class may not extend Throwable, because catch clauses need the exact type at run time and T would be erased Correct
  • C Exception classes cannot have fields
  • D serialVersionUID must be public
Correct Answer: Option B

Store the bad value as Object (or make specific exception classes).

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links