简体   繁体   中英

how to ignore the exception and continue to next value

Hi I am trying to pass the list of values to google api.If the api throws any exceptions, it comes out of the loop.I need to coninue to next value even if throws any error.My code below.

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;
   }

even If I mention return null instead of throw new exception.It shows null pointer exception.any help will be appreciated.

put try/catch around this part

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

Try

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

You should put those statements where you expect an exeption to happen in a try block and process those exceptions in a catch block.

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
}

To clarify: the try..catch statement creates a block of code in which, should an exception occur, that exception will be "caught." (You can nest these statements, and, in the catch block, specify specific exception-types that you wish to catch ...)

After the statement completes (unless you "re-raise" an exception within the catch ...), the exception is "gone."

There are plenty of examples of how this statement works, eg in the Java documentation itself. (And, for the record, almost every programming language out there supports some flavor of this essential concept.)

Note that you must put the entire try..catch statement fully inside the loop. Do not attempt to "continue the loop" from the catch handler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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