By Domenica


2019-01-11 11:37:41 8 Comments

I'm reading the error log file and I'm checking if error line refers to the annotation. I tried on the doc without results, but can the annotation (or a custom annotation) throw an exception?

Thanks a lot.

3 comments

@J-Alex 2019-01-11 12:27:54

Annotation can be a CAUSE of the exception, but cannot throw it hence it's just a marker for an Annotation Processor.

This is a dummy example how an Annotation can be a cause of an Exception:

public class Main {

    @ValidNumber
    public String number = "1234X5";

    public static void main(String[] args) {
        new AnnotationProcessor().scan();
    }
}

class AnnotationProcessor {
    private String regex = "\\d+";

    void scan() {
        Main m = new Main();
        for (Field field : m.getClass().getDeclaredFields()) {
            ValidNumber annotation = field.getAnnotation(ValidNumber.class);
            if (annotation != null)
                process(m, field);
        }
    }

    void process(Object target, Field field) {
        try {
            Object o = field.get(target);
            if (!o.toString().matches(regex))
                throw new IllegalStateException("Wrong number in the class " + target);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface ValidNumber {
}

Output:

Exception in thread "main" java.lang.IllegalStateException: Wrong number in the class [email protected]
    at com.test.AnnotationProcessor.process(Main.java:32)
    at com.test.AnnotationProcessor.scan(Main.java:24)
    at com.test.Main.main(Main.java:12)

This is an example how to process annotation an RUNTIME.

@GhostCat 2019-01-11 12:28:59

Nice approach regarding wording: that difference between being the source and the cause of an exception.

@GhostCat 2019-01-11 11:53:41

Annotations can be processed in two ways:

  • at compile time: they actually result in different/additional byte code being written into class files
  • at run time: typically, some sort of framework checks for the presence of an annotation on a method/class/... to then run different code, depending if that specific annotation is in place or not

In other words: the annotation itself doesn't resemble to any real code, it is just a "marker". But of course, in order to do something useful (especially at runtime), there is probably some code that does something different when annotations are present. And of course, that code can throw exceptions. But in that case, you should receive a stack trace, and hopefully helpful messages.

From that point of view: the annotation itself can't throw an exception, because the annotation itself doesn't resemble something that can be "executed". Or to steal wording from the other answer by J-Alex: annotations can cause exceptions, but they can't be the "source".

@J-Alex 2019-01-11 12:29:33

Actually, in three ways, if they discarded by compilers having RetentionPolicy.SOURCE, the only way to process them is to read sources.

@GhostCat 2019-01-11 12:30:32

@J-Alex That is still case "one". The annotation is processed at compile time. It doesn't matter if the annotation itself is added to the compiled class file or not, regarding that aspect.

@leftbit 2019-01-11 11:48:53

Annotations are not executed, hence they can't throw an exception.

Related Questions

Sponsored Content

21 Answered Questions

[SOLVED] How do I check if a file exists in Java?

  • 2009-11-29 20:32:39
  • DVK
  • 1017162 View
  • 728 Score
  • 21 Answer
  • Tags:   java file-io io

23 Answered Questions

34 Answered Questions

21 Answered Questions

20 Answered Questions

[SOLVED] Which @NotNull Java annotation should I use?

9 Answered Questions

24 Answered Questions

17 Answered Questions

[SOLVED] How slow are Java exceptions?

12 Answered Questions

[SOLVED] How to upload files to server using JSP/Servlet?

32 Answered Questions

[SOLVED] The case against checked exceptions

Sponsored Content