2012-08-06 101 views
-1

代碼:如何捕捉超時異常

Class Manager { 
    Future fu = pool.invokeAll(workers, SEARCH_TIMEOUT, TimeUnit.SECONDS); 
    // calling the invoke call 
    search search= fu.get();  
    // callable 
} 


public class Search implements Callable<Search> { 
    Search call() { 
     // multiple workers will execute Code So don't want to catch timed out exception in here 
     // api value will be changing based on corresponding reference 
     api.search_api(); 
    } 
} 


class api() 
{ 
    search_api(){ 
     // How to catch a timed out exception in here 
     // catch(TimedoutException){} did not work in here 
    } 
} 

有沒有辦法,我能趕上在類的API的TIMEDOUT例外情況的方法search_api()的方法嗎?

回答

1

你能趕上TimeoutException這樣的代碼:

try { 
     Future fu = pool.invokeAll(workers, SEARCH_TIMEOUT, 
       TimeUnit.SECONDS); 
     // calling the invoke call 
     search search = fu.get(); 
    } catch (TimeoutException e) { 
     e.printStackTrace(); 
    } 
0

您也可以通過以下方式

try{ 

. 

. 

. 

. 


catch (RuntimeException e) { 
     // TODO: handle exception 
}