私は 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
}
しかし、エラーが発生し、SQLException が決してスローされないという IDE のメッセージが表示されました。これは、ExecutorService で実行しているためですか?
UPDATE : したがって、送信は 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
}