Reputation: 2920
I am using an annotation that only makes sense on final classes (among other things). While I am processing this annotations I would like to throw some kind of exception that says, the class is not final so the combination of this annotation and this class don't make sense. Is there an exception that Oracle would recommend to use?
Upvotes: 0
Views: 126
Reputation: 48644
The case you are interested in is a situation in which the programmer has incorrectly placed an annotation. That is, the text of the program is buggy. A RuntimeException
or a class derived from it would therefore be most appropriate.
It must be an unchecked exception, because demanding the program catch an exception that can only be caused by a bug in the program is unreasonable; there is no corrective action the program can take, it might as well crash and print a stack-trace to help the programmer debug the problem, so you would never want to catch the exception.
As no code should catch the specific type of exception, it otherwise does not matter which class the exception has. The class name just provides some additional diagnostic information about the failure. You could repurpose the existing TypeConstraintException
.
Upvotes: 1