我有這個在實現可調用的類:爲什麼我得到一個錯誤,說沒有人拋出異常?
public class MasterCrawler implements Callable {
public Object call() throws SQLException {
resumeCrawling();
return true;
}
//more code with methods that throws an SQLException
}
在這種執行該贖回,像這樣其他類:
MasterCrawler crawler = new MasterCrawler();
try{
executorService.submit(crawler); //crawler is the class that implements Callable
}(catch SQLException){
//do something here
}
但我得到了一個錯誤和IDE的消息SQLException永遠不會拋出。這是因爲我在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
}
Callable引發異常。 http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/Callable.html – 2011-05-27 02:34:32