2

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?

Renato Dinhani
  • 35,057
  • 55
  • 139
  • 199

1 Answers1

2

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.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • Yes, thanks, I think is this, I removed the `get()` method. Thanks for your explanation. I don't know why, but I thought that only the results will came with the get and the explanation will be thrown at any time. Now I can solve this problem. – Renato Dinhani Oct 21 '11 at 03:20
  • @RenatoDinhaniConceição One reason it's good to provide as much info as possible when asking :) – Dave Newton Oct 21 '11 at 04:17