Scala @ raises a few exceptions

I need to call scala code from java, so I need to tell the compiler that a specific method throws certain exceptions. This is easy to do for one exception , but I'm struggling to declare that the method throws multiple exceptions.

This does not work:

@throws( classOf[ ExceptionA ], classOf[ExceptionB] ) 

And obviously this does not:

 @throws( classOf[ ExceptionA , ExceptionB] ) 
+6
source share
2 answers

When looking at the constructor for @throws it takes one argument to Class[_] . Given this, you cannot use array notation to represent multiple classes. Thus, an alternative to add annotations several times, one for each supported exception:

 @throws( classOf[ExceptionA] ) @throws( classOf[ExceptionB] ) 
+9
source

@throws defined as follows:

 class throws[T <: Throwable](cause: String = "") extends scala.annotation.StaticAnnotation {...} 

This way you can put only one exception in the annotation. Add one annotation to the exception.

+4
source

All Articles