繁体   English   中英

Java:为什么在Second Method catch块内未捕获异常

[英]Java : Why exception is not being caught inside the Second Method catch block

我有这种方法调用下面的方法,我故意为要连接的URL提供错误的端口号。 但是令我惊讶的是,在第一个Method catch块中捕获了产生的异常,为什么不在executeData Method的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;
    }

那可能是因为你的台词

// some code

产生另一个被捕获的异常。 您的第二种方法不会引发任何异常,我认为这没有错。

connection.connect();

会抛出异常。

您还可以检查状态:

int responseCode = connection.getResponseCode();
if (responseCode != 200) {
    // Not OK.
}

我猜异常发生在

//一些代码//一些代码

executeData catch块executeData

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM