Renato Dinhani
Renato Dinhani

Reputation: 36746

Why the exception is not being thrown?

I have this simple piece of code:

@Override
public Object call() throws Exception {
    try (Connection conn = ConnectionPool.getConnection()) {
        pageDAO = new PageDAO(conn);
        linkDAO = new LinkDAO(conn);
        loopInsertion();
    }
    return true;
}

I'm getting a SQLException in the getConnection() method. If I put a catch, the exception is catched in the block, but if not, the Exception is not throwed ahead, but an error not occurs. Appears that it became locked and not continues the code execution.

Why this behavior? I misunderstood something? This is not expected to do?

Upvotes: 2

Views: 242

Answers (1)

Ryan Stewart
Ryan Stewart

Reputation: 128929

I'm guessing about the code that you haven't shown, but if this is a Callable that you invoke with an ExecutorService, any exception that happens inside the Callable code won't be propagated to anywhere until you call one of the get() methods on the Future that was returned when you submitted the Callable. When you invoke get(), it will throw an ExecutionException whose root cause is the exception that was thrown from your code.

To put it more simply, when you fork code to another thread using an ExecutorService, any exception thrown from that code is caught and held until you come back and ask for the result of running the code. If you never do, then the exception just disappears.

Upvotes: 2

Related Questions