goncalopp goncalopp - 1 year ago 103
Java Question

Catch in Java a exception thrown in Scala - unreachable catch block

Scala doesn't have checked exceptions. However, when calling scala code from java, it's desirable to catch exceptions thrown by scala.


def f()=
//do something that throws SomeException


{ f() }
catch (SomeException e)

javac doesn't like this, and complains that "this exception is never thrown from the try statement body"

Is there a way to make scala declare that it throws a checked exception?

Answer Source

Use a throws annotation:

def f()= {
    //do something that throws SomeException

You can also annotate a class constructor:

class MyClass @throws(classOf[SomeException]) (arg1: Int) {

This is covered in the Tour of Scala

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download