throws

How to use @SerialVersionUID and other Scala annotations

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 17.3, “How to use @SerialVersionUID and other Scala annotations.”

Problem

You want to specify that a class is serializable, and set the serialVersionUID. More generally, you want to know the syntax for using annotations in your Scala code, and know which annotations are available.

Solution

Use the Scala @SerialVersionUID annotation while also having your class extend the Serializable trait:

How to add exception annotations to Scala methods so they can be called from Java alvin June 20, 2015 - 7:03pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 17.2, “How to add exception annotations to Scala methods so they can be called from Java.”

Problem

You want to let Java users know that a method can throw one or more exceptions so they can handle those exceptions with try/catch blocks.

Solution

Add the @throws annotation to your Scala methods so Java consumers will know which methods can throw exceptions and what exceptions they throw.

How to generate Scala documentation with the ‘scaladoc’ command

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 14.8, “How to generate Scala documentation with the scaladoc command.”

Problem

You’ve annotated your Scala code with Scaladoc, and you want to generate developer documentation for your API.

How to declare that a Scala method can throw an exception

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 5.8, “How to declare that a Scala method can throw an exception.”

Problem

You want to declare that a method can throw an exception, either to alert callers to this fact or because your method will be called from Java code.

Solution

Use the @throws annotation to declare the exception(s) that can be thrown. To declare that one exception can be thrown, place the annotation just before the method signature: