Index: /trunk/src/org/openstreetmap/josm/gui/preferences/imagery/AddWMSLayerPanel.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/gui/preferences/imagery/AddWMSLayerPanel.java	(revision 10519)
+++ /trunk/src/org/openstreetmap/josm/gui/preferences/imagery/AddWMSLayerPanel.java	(revision 10520)
@@ -84,7 +84,9 @@
                     formats.setSelectedItem(wms.getPreferredFormats());
                 } catch (MalformedURLException ex) {
+                    Main.error(ex, false);
                     JOptionPane.showMessageDialog(getParent(), tr("Invalid service URL."),
                             tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
                 } catch (IOException ex) {
+                    Main.error(ex, false);
                     JOptionPane.showMessageDialog(getParent(), tr("Could not retrieve WMS layer list."),
                             tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
@@ -93,10 +95,12 @@
                     String title = tr("WMS Error");
                     String message = tr("Could not parse WMS layer list.");
-                    Main.error("Could not parse WMS layer list. Incoming data:\n"+incomingData);
-                    if (incomingData != null
-                            && (incomingData.startsWith("<html>") || incomingData.startsWith("<HTML>"))
-                            && (incomingData.endsWith("</html>") || incomingData.endsWith("</HTML>"))) {
+                    Main.error(ex, "Could not parse WMS layer list. Incoming data:\n"+incomingData);
+                    if ((incomingData.startsWith("<html>") || incomingData.startsWith("<HTML>"))
+                      && (incomingData.endsWith("</html>") || incomingData.endsWith("</HTML>"))) {
                         GuiHelper.notifyUserHtmlError(AddWMSLayerPanel.this, title, message, incomingData);
                     } else {
+                        if (ex.getMessage() != null) {
+                            message += '\n' + ex.getMessage();
+                        }
                         JOptionPane.showMessageDialog(getParent(), message, title, JOptionPane.ERROR_MESSAGE);
                     }
Index: /trunk/src/org/openstreetmap/josm/io/imagery/WMSImagery.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/io/imagery/WMSImagery.java	(revision 10519)
+++ /trunk/src/org/openstreetmap/josm/io/imagery/WMSImagery.java	(revision 10520)
@@ -40,4 +40,9 @@
         private final String incomingData;
 
+        /**
+         * Constructs a new {@code WMSGetCapabilitiesException}
+         * @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method)
+         * @param incomingData the answer from WMS server
+         */
         public WMSGetCapabilitiesException(Throwable cause, String incomingData) {
             super(cause);
@@ -45,4 +50,19 @@
         }
 
+        /**
+         * Constructs a new {@code WMSGetCapabilitiesException}
+         * @param message   the detail message. The detail message is saved for later retrieval by the {@link #getMessage()} method
+         * @param incomingData the answer from the server
+         * @since 10520
+         */
+        public WMSGetCapabilitiesException(String message, String incomingData) {
+            super(message);
+            this.incomingData = incomingData;
+        }
+
+        /**
+         * Returns the answer from WMS server.
+         * @return the answer from WMS server
+         */
         public String getIncomingData() {
             return incomingData;
@@ -160,7 +180,13 @@
             });
             Document document = builder.parse(new InputSource(new StringReader(incomingData)));
+            Element root = document.getDocumentElement();
+
+            // Check if the request resulted in ServiceException
+            if ("ServiceException".equals(root.getTagName())) {
+                throw new WMSGetCapabilitiesException(root.getTextContent(), incomingData);
+            }
 
             // Some WMS service URLs specify a different base URL for their GetMap service
-            Element child = getChild(document.getDocumentElement(), "Capability");
+            Element child = getChild(root, "Capability");
             child = getChild(child, "Request");
             child = getChild(child, "GetMap");
@@ -197,5 +223,5 @@
             }
 
-            Element capabilityElem = getChild(document.getDocumentElement(), "Capability");
+            Element capabilityElem = getChild(root, "Capability");
             List<Element> children = getChildren(capabilityElem, "Layer");
             layers = parseLayers(children, new HashSet<String>());
@@ -327,7 +353,9 @@
     private static List<Element> getChildren(Element parent, String name) {
         List<Element> retVal = new ArrayList<>();
-        for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
-            if (child instanceof Element && name.equals(child.getNodeName())) {
-                retVal.add((Element) child);
+        if (parent != null) {
+            for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
+                if (child instanceof Element && name.equals(child.getNodeName())) {
+                    retVal.add((Element) child);
+                }
             }
         }
