如何忽略异常并继续下一个值

时间:2016-08-08 12:08:36

标签: java

您好我正在尝试将值列表传递给google api。如果api抛出任何异常,它就会出现循环。即使抛出任何错误,我也需要继续下一个值。我的代码如下。

while (iterator.hasNext()) {
       Object element = iterator.next();
       String postcode = element.toString().trim();
       String latLongs[] = getLatLongPositions(postcode);
       System.out.println("Latitude: " + latLongs[0] + " and Longitude: " + latLongs[1]);
       System.out.println(postcode);
   }

   public static String[] getLatLongPositions(String address) throws Exception {

       int responseCode = 0;
       String api = "http://maps.googleapis.com/maps/api/geocode/xml?address=" + URLEncoder.encode(address, "UTF-8") + "&sensor=true";
       URL url = new URL(api);
       HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
       httpConnection.connect();
       responseCode = httpConnection.getResponseCode();
       if (responseCode == 200) {
           DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();;
           org.w3c.dom.Document document = builder.parse(httpConnection.getInputStream());
           XPathFactory xPathfactory = XPathFactory.newInstance();
           XPath xpath = xPathfactory.newXPath();
           XPathExpression expr = xpath.compile("/GeocodeResponse/status");
           String status = (String) expr.evaluate(document, XPathConstants.STRING);
           if (status.equals("OK")) {
               expr = xpath.compile("//geometry/location/lat");
               String latitude = (String) expr.evaluate(document, XPathConstants.STRING);
               expr = xpath.compile("//geometry/location/lng");
               String longitude = (String) expr.evaluate(document, XPathConstants.STRING);
               return new String[] {
                   latitude, longitude
               };
           } else {

               throw new Exception("Error from the API - response status: " + status);
           }
       }
       return null;
   }

即使我提到return null而不是throw new exception.It显示空指针异常。任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:2)

把try / catch放在这个部分

...
try{
    String latLongs[] = getLatLongPositions(postcode);
    System.out.println("Latitude: " + latLongs[0] + " and Longitude: " + latLongs[1]);
    System.out.println(postcode);
}catch(Exception e){
    ...
}

答案 1 :(得分:0)

尝试

else {
               try {
            throw new Exception ("Error from the API - response status: " + status);
                } catch (Exception e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
    }

答案 2 :(得分:0)

您应该将这些语句放在try块的预期位置,并在catch块中处理这些异常。

try {
 // statement where an exception may occur
}
catch ( SomeTypeOfException e ) {
 // will be processed if this particular exception occured
} 
catch ( Exception e )  {
 // process any exception
}
finally {
 // do this regardless of what happened in the try block
}

答案 3 :(得分:0)

澄清一下:try..catch语句创建了一个代码块,如果发生异常,该异常将被“捕获”。 (您可以嵌套这些语句,并在catch块中指定您希望捕获的特定异常类型...)

语句完成后(除非你在catch中“重新引发”异常......),异常就“消失了。”

在Java文档本身中有很多关于这个语句如何工作的例子,例如(并且,为了记录,几乎所有的编程语言都支持这个基本概念的一些风格。)

请注意,您必须将整个 try..catch语句完全放在循环中。不要试图从catch处理程序“继续循环”。