我有這種方法調用下面的方法,我有意提供一個錯誤的端口號來連接URL。 但令我吃驚的是,產生的異常被捕獲到第一個方法catch塊 爲什麼它沒有在executeData方法的catch塊內處理?Java:爲什麼在第二個方法catch塊中沒有捕獲異常
**1st Method**
public APIResponse execute(Request request, Class<? extends Response> responseClass) {
try {
String xmlResponse = executeData(request);
// some code
return response;
}
catch (Exception ex) {
return new Response(ErrorCode.SYSTEM_ERROR);
}
}
2nd Method
public String executeData(Request request) throws IOException {
URL url = null;
URLConnection urlc = null;
try {
url = new URL("http://localhost:80870/");
urlc = url.openConnection();
}
catch (Exception ex) {
ex.printStackTrace();
**// This is not being executed .**
}
// Some code
// Some code
return xmlResponse;
}
你絕對確定沒有執行數據異常處理程序中的g會在您看到執行代碼的結果之前拋出異常? – 2012-01-10 11:59:47
如果你從第二種方法中移除了'throws IOException',它會起作用嗎? – Viruzzo 2012-01-10 12:09:56
@Viruzo,沒有幫助,如果我刪除拋出 – Kiran 2012-01-10 12:20:27