为什么我收到一条错误消息,说没有抛出任何异常?

发布于 2024-11-10 04:00:50 字数 1130 浏览 0 评论 0 原文

我在一个实现 Callable 的类中有这个:

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

在执行这个 Callable 的其他类中,类似这样:

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

但我收到一个错误,并且 IDE 收到一条消息,提示 SQLException 永远不会抛出。这是因为我正在 ExecutorService 中执行?

更新:因此提交不会抛出 SQLException。我该如何执行 Callable (作为线程运行)并捕获异常?

已解决:

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
}

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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

2024-11-17 04:00:50

当您调用 submit 时,您正在传递一个对象。您没有调用 call()

编辑

提交返回未来 f.当您调用 f.get() 时,该方法可能会抛出 ExecutionException 如果在可调用的执行过程中遇到问题。如果是这样,它将包含 call() 抛出的异常。

通过将 Callable 提交给执行器,您实际上是在要求它执行(异步)。无需采取进一步行动。只需检索未来并等待即可。

关于解决方案

虽然您的解决方案可以工作,但这不是很干净的代码,因为您劫持了 Call 的返回值。尝试这样的事情:

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

    } 

}

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

    } 

}
双手揣兜 2024-11-17 04:00:50

提交方法不会抛出 SQLException。

The submit method does not throw a SQLException.

勿挽旧人 2024-11-17 04:00:50

这是因为爬虫永远不会抛出 SQLException。

尝试使用 finally 而不是 catch 并查看是否会遇到问题或是否有效。

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.

裸钻 2024-11-17 04:00:50

你使用什么IDE?当我尝试你的代码时,Eclipse 抱怨“未处理的异常类型异常”。这是有道理的,因为 Callable 接口定义了 call() 方法来抛出 Exception。仅仅因为您的实现类声明了更受限制的异常类型,调用程序就不能依赖它。它希望您捕获异常。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文