Catch HttpHostConnectException e

4407
5
Jump to solution
11-13-2015 10:37 AM
EdgarCanul
New Contributor III

According to Not able to add ArcGISDynamicMapServiceLayer in Arc

When you don't properly set the proxy parameters you get HttpHostConnectException. How can you catch this connection exception (org.apache.http.conn.HttpHostConnectException) since it's not thrown by JMap class itself. I need to catch this error and take suitable actions or an appropriate warning message about the proxy parameters correctness.

I tried:

MapOptions mapOptions = new MapOptions(MapType.STREETS, 24.43261, -102.92041, 4);

try{

JMap jMap = new JMap(mapOptions);

  jMap.setWrapAroundEnabled(true);

}

catch(HttpHostConnectException e){

   // Handle error connection due to incorrect proxy parameters

}

But, of course, the compiler claims "Out of Reach try catch clause"

0 Kudos
2 Solutions

Accepted Solutions
VijayGandhi
New Contributor III

You could get the exception using the getException() method on the layer. This method could be invoked after the layer has initialized.

    dynamicLayer = new ArcGISDynamicMapServiceLayer(URL_DYNAMIC_LAYER);
    dynamicLayer.addLayerInitializeCompleteListener(new LayerInitializeCompleteListener() {
      @Override
      public void layerInitializeComplete(LayerInitializeCompleteEvent e) {
        System.out.println("Error: " + dynamicLayer.getException());
      }
    });

View solution in original post

EdgarCanul
New Contributor III

OK Vijay. You are right. I finally could implement your suggestion. Thanks a lot. Now I'm going to try to accomodate to my app.

View solution in original post

5 Replies
EricBader
Occasional Contributor III

First of all, good "catch", by the way.

What happens when you try to catch a general Exception?

This is what works for me. Surround your layer definition code with a try/catch for Exception, instead of a specific one. I know it sounds improper, but this may get you farther along, hopefully?

0 Kudos
EdgarCanul
New Contributor III

Nothing happens, I used:

try{

     MapOptions mapOptions = new MapOptions(MapType.STREETS, 24.43261, -102.92041, 4);

     JMap jMap = new JMap(mapOptions);

      jMap.setWrapAroundEnabled(true);

}

catch(Exception e){

     JOptionPane.showMessageDialog(window, "Error connection");

}

It doesn't catch it.

I think the error connection occurs when creating the map because when I debug it, is in this step when the exception is thrown (after 20 seconds). But when I checked the constructor new JMap it doesn't throw any exception.

Please, help.

0 Kudos
VijayGandhi
New Contributor III

You could get the exception using the getException() method on the layer. This method could be invoked after the layer has initialized.

    dynamicLayer = new ArcGISDynamicMapServiceLayer(URL_DYNAMIC_LAYER);
    dynamicLayer.addLayerInitializeCompleteListener(new LayerInitializeCompleteListener() {
      @Override
      public void layerInitializeComplete(LayerInitializeCompleteEvent e) {
        System.out.println("Error: " + dynamicLayer.getException());
      }
    });
EdgarCanul
New Contributor III

Sorry, could you give me more clues. I couldn't figure out how to use it. Since I don't use DynamicMapServiceLayer, according to my last snippet I tried:

  MapOptions mapOptions = new MapOptions(MapType.STREETS, 17.94262710, -95.92216487, 6);
  map = new JMap(mapOptions);
  map.getLayers().get(0).addLayerInitializeCompleteListener(new LayerInitializeCompleteListener() {
          @Override
            public void layerInitializeComplete(LayerInitializeCompleteEvent e) {
                    System.out.println("Error: " + map.getLayers().get(0).getException().getMessage());
                    }
            });

Since, after creating, the map the layer[0] is the BaseMapTiled Layer. But I still can't catch the exception, I think that's because at that point the layer doesn't exist due to the connection error.

0 Kudos
EdgarCanul
New Contributor III

OK Vijay. You are right. I finally could implement your suggestion. Thanks a lot. Now I'm going to try to accomodate to my app.