Search code examples
javaruntimeexceptionannotations-processing-messager

Java: Messager vs runtime exception


In annotation processing, I am looking for a general guide as to when to use Messager, with Diagnostic.Kind.ERROR and when to use runtime exception. I know that using Messager will raise an error in the environment.

Let's consider this example. I have an annotation that is put on top of interfaces. The job of its processor is to make sure that all the methods wherein have a unique identifier of a specific type. If the processor encounters a violation, should it use Messager, or should it throw a runtime exception?

One disadvantage of Messager is that if that violation causes other issues in the program and other processors, the error message gets lost in other error messages and pollutes the logging.


Solution

  • I think this paragraph from the documentation of Processor is a good general guide.

    If a processor throws an uncaught exception, the tool may cease other active annotation processors. If a processor raises an error, the current round will run to completion and the subsequent round will indicate an error was raised. Since annotation processors are run in a cooperative environment, a processor should throw an uncaught exception only in situations where no error recovery or reporting is feasible.