Catching UnknownHostException of RetrievalUrl in W

2019-07-18 01:46发布

问题:

I use WorldWindGlCanvas in a netbeans TopComponent. When top component is opened WorldWInd try to connect to some url (for example worldwind20.arc.nasa.gov). If there is not internet connection UnknowHostException is occured and a dialog is shown to show this exception. I want to catch this exception. Note that I know that worldwind could work offline and I can set it work offline but I want to set worldwind online so that it use online tiles when internet connection is provided and it uses cached tiles if there is no internet connection. Is there any way to catch this exception?

回答1:

Looking at the source code for World Wind, there doesn't appear to be a way to catch that exception.

Upon manually disconnecting my Internet connection, I received a stack trace of the following:

Jun 16, 2017 6:19:43 PM 
gov.nasa.worldwind.util.SessionCacheRetrievalPostProcessor run
SEVERE: Retrieval failed for https://worldwind26.arc.nasa.gov/elev?EXCEPTIONS=application/vnd.ogc.se_xml&REQUEST=GetCapabilities&SERVICE=WMS&VERSION=1.3.0
Jun 16, 2017 6:19:43 PM gov.nasa.worldwind.util.SessionCacheUtils retrieveSessionData
SEVERE: Exception while retrieving resources for https://worldwind26.arc.nasa.gov/elev?EXCEPTIONS=application/vnd.ogc.se_xml&REQUEST=GetCapabilities&SERVICE=WMS&VERSION=1.3.0
java.net.UnknownHostException: worldwind26.arc.nasa.gov
...
at gov.nasa.worldwind.retrieve.HTTPRetriever.doRead(HTTPRetriever.java:48)
at gov.nasa.worldwind.retrieve.URLRetriever.read(URLRetriever.java:368)
at gov.nasa.worldwind.retrieve.URLRetriever.call(URLRetriever.java:244)
at gov.nasa.worldwind.retrieve.URLRetriever.call(URLRetriever.java:27)
at gov.nasa.worldwind.util.SessionCacheUtils.retrieveSessionData(SessionCacheUtils.java:80)
at gov.nasa.worldwind.util.SessionCacheUtils.getOrRetrieveSessionCapabilities(SessionCacheUtils.java:170)
at gov.nasa.worldwind.terrain.BasicElevationModel.retrieveResources(BasicElevationModel.java:2028)
at gov.nasa.worldwind.terrain.BasicElevationModel$3.run(BasicElevationModel.java:2118)
at java.lang.Thread.run(Thread.java:745)

Based on that stack trace, I investigated a few source files:

URLRetriever.java:

try {
        ...
} catch (Exception e) {
        if (!(e instanceof java.net.SocketTimeoutException || e instanceof UnknownHostException
            || e instanceof SocketException)) {
                Logging.logger().log(Level.SEVERE,
                Logging.getMessage("URLRetriever.ErrorReadingFromConnection", this.url.toString()), e);
        }
        throw e;
}

SessionCacheUtils.java:

try {
        retriever.call();
    } catch (Exception e) {
        String message = Logging.getMessage("layers.TiledImageLayer.ExceptionRetrievingResources", url.toString());
        Logging.logger().log(java.util.logging.Level.SEVERE, message, e);
    }

It appears to be handled internally, and thus you seem to be out of luck.