Scala Language Annotations Annotating the main constructor


Example

/**
 * @param num Numerator
 * @param denom Denominator
 * @throws ArithmeticException in case `denom` is `0`
 */
class Division @throws[ArithmeticException](/*no annotation parameters*/) protected (num: Int, denom: Int) {
    private[this] val wrongValue = num / denom
    
    /** Integer number
     *  @param num Value */
    protected[Division] def this(num: Int) {
      this(num, 1)
    }
}
object Division {
  def apply(num: Int) = new Division(num)
  def apply(num: Int, denom: Int) = new Division(num, denom)
}

The visibility modifier (in this case protected) should come after the annotations in the same line. In case the annotation accepts optional parameters (as in this case @throws accepts an optional cause), you have to specify an empty parameter list for the annotation: () before the constructor parameters.

Note: Multiple annotations can be specified, even from the same type (repeating annotations).

Similarly with a case class without auxiliary factory method (and cause specified for the annotation):

case class Division @throws[ArithmeticException]("denom is 0") (num: Int, denom: Int) {
    private[this] val wrongValue = num / denom
}