Renato Dinhani
Renato Dinhani

Reputation: 36746

Why I got an error saying that no one exception is thrown?

I have this in a class that implements Callable :

public class MasterCrawler implements Callable {
    public Object call() throws SQLException {
        resumeCrawling();
        return true;
    }
    //more code with methods that throws an SQLException
}

In other class that execute this Callable, something like this:

MasterCrawler crawler = new MasterCrawler();
try{
    executorService.submit(crawler); //crawler is the class that implements Callable
}(catch SQLException){
    //do something here
}

But I got an error and a message of the IDE that an SQLException is never throw. This is because I'm executing in a ExecutorService?

UPDATE: So the submit don't throws an SQLException. How I can do to execute the Callable (run as thread) and catch the exception?

SOLVED:

public class MasterCrawler implements Callable {
    @Override
    public Object call() throws Exception {
        try {
            resumeCrawling();
            return true;
        } catch (SQLException sqle) {
            return sqle;            
        }
     }
}


Future resC = es.submit(masterCrawler);
if (resC.get(5, TimeUnit.SECONDS) instanceof SQLException) {
    //do something here
}

Upvotes: 1

Views: 624

Answers (4)

When you call submit, you are passing an object. You are not calling call().

EDIT

Submit returns a Future f. When you call f.get(), the method can throw an ExecutionException if a problem is encountered during the execution of the callable. If so, it will contain the exception thrown by call().

By submitting your Callable to the executor, you are actually asking it to execute it (asynchronously). No need for further action. Just retrieve the future and wait.

ABOUT THE SOLUTION

Although your solution will work, this not very clean code, because you are hijacking the return value of Call. Try something like this:

public class MasterCrawler implements Callable<Void> {

    @Override
    public Void call() throws SQLException {
        resumeCrawling();
        return null;
    }

    public void resumeCrawling() throws SQLException {
        // ... if there is a problem
        throw new SQLException();
    }    

}

public void doIt() {

    ExecutorService es = Executors.newCachedThreadPool();
    Future<Void> resC = es.submit(new MasterCrawler());

    try {

        resC.get(5, TimeUnit.SECONDS);
        // Success

    } catch ( ExecutionException ex ) {

        SQLException se = (SQLException) ex.getCause();
        // Do something with the exception

    } catch ( TimeoutException ex ) {

        // Execution timed-out

    } catch ( InterruptedException ex ) {

        // Execution was interrupted

    } 

}

Upvotes: 2

Ted Hopp
Ted Hopp

Reputation: 234857

What IDE are you using? When I try your code, Eclipse complains "unhandled exception type Exception". This makes sense because the Callable interface defines the call() method to throw Exception. Just because your implementation class declares a more restricted exception type, the calling program cannot count on that. It expects you to catch Exception.

Upvotes: 0

David Rodrigues
David Rodrigues

Reputation: 12552

It's because SQLException never will be throw by the crawler.

Try use finally instead of catch and see if you will have a problem or it works.

Upvotes: 0

ditkin
ditkin

Reputation: 7054

The submit method does not throw a SQLException.

Upvotes: 1

Related Questions