简体   繁体   中英

Checked exceptions in Java when calling Groovy code?

When I have a Groovy class (in the groovy folder and ending in .groovy):

...
DerbyNow() throws IOException {
    // create socket connection
    socket = new EventSocket(new Socket(DERBY_NOW_HOST, DERBY_NOW_PORT)) // EventSocket throws IOException as well
    ...

and create it from Java code (in the java folder and ending in .java):

try {
    DerbyNow derbyNow = new DerbyNow();

    setDerbyNow(derbyNow);
} catch (IOException e) {
    Log.bad("error while setting up Derby Now: ", e);
}

IntelliJ IDEA doesn't give me an error/warning (correct), but when I go to compile/run, it gives me an error in the Messages Make view (incorrect):

Error:(382, 19) java: exception java.io.IOException is never thrown in body of corresponding try statement

How can I get it so that the compiler recognizes this exception? I looked at the decompiled groovy class (decompiled to Java), and it does still throw the IOException .

Here's part of my Gradle file:

apply plugin: 'java'
apply plugin: 'groovy'

...

repositories {
    mavenCentral()
    mavenLocal()
    ...
}

...

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.3.11'
    ...
}

I am using the javac compiler (default).

I wish I had a clear answer from a Groovy specialist. I had a similar issue and worked around it in a very ugly way:

try {
    DerbyNow derbyNow = new DerbyNow();

    setDerbyNow(derbyNow);
} catch (Exception e) {
    if (e instanceOf IOException) {
        Log.bad("error while setting up Derby Now: ", e);
    } else {
        throw e;
    }
}

So that works because since Java7, there is some special forgiveness for Exception .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM