What are the advantages of the @throws annotation?

As far as I understand, it is used to generate a throws clause for Java applications to see.
Is there any benefit in using it if one doesn’t support Java client code? I have been using it for about two years now I think, because I noticed it in Scala sources. So far, I see only disadvantages compared to the @throws tag in Scaladoc:

  • Scaladocs can be folded in the IDE while annotations not, meaning even small methods have a large on screen footprint.
  • Scaladocs supports formatting, annotations do not.
  • Scaladoc can use @define macros which allow generating more meaningful phrasing when the method is inherited from a very generic mix in.
  • Scaladocs can be inherited.

If we are using a code structure, I would like at least to have @throws being inheritable - possibly with an additional parameter, unless an overriding method already defines some @throws annotations or @throws[Nothing].

I am seriously thinking of migrating back to the use of the Scaladoc tag unless someone enlightens me of why this is not preferred.

1 Like

I also have the same question and I am surprised no one has answered it yet.

Also, would the Java compile really check for the throws annotation? I don’t think Java can read annotations created in Scala. This is even explained in the documentation for StaticAnnotations

AFAIK it’s only useful if you’re writing code that should be used from Java. The Java compiler doesn’t check for this annotation, but the Scala compiler transforms it to the same bytecode that Java uses for its throws clause for checked exceptions.

1 Like

similar to how the @volatile and @native annotations actually change the bytecode signature, and do not appear as annotations to java

1 Like

Doest that mean that if I use the compiled class (that was compiled with the scala compiler) in java only project as a dependency, this dependency would be interpreted as a class with a checked exception by the java compiler?

yeah exactly right - equivalent to being compiled as throws Foo in a Java method

1 Like