Question

I am using Lombok's @Cleanup and even when I find it very useful, I cannot figure out how can I catch an exception being thrown by the main block of code that makes use of the resource to be cleant.

My code is the following:

ScriptRunner runner = null;
try {
    runner = scriptRunner();

    for (Resource sqlFile : sqlScripts) {
        runScript(sqlFile.getFile(), runner);
    }
} catch (Exception ex) {
    log.error("Error found when attempting to run script", ex);
} finally {
    if (runner != null) {
        runner.closeConnection();
    }
}

With Lombok, I could simplify the code above this way:

@Cleanup("closeConnection") ScriptRunner runner = scriptRunner();

for (Resource sqlFile : sqlScripts) {
    runScript(sqlFile.getFile(), runner);
}

But I would be missing the possibility of catching any exception that might arise when the for loop executes. Is there any way to do this in an elegant way with Lombok? (i.e. other than programmatically using "try /catch?)

Thanks very much in advance.

Était-ce utile?

La solution

No. If you want to process the exception you need to catch it, and for that you'll need a try-catch block.

Full Disclosure: I am one of the Project Lombok developers.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top