在异常后继续列表迭代

时间:2017-03-06 09:24:23

标签: java list arraylist iteration try-catch

            List<String> assignedGroups = new ArrayList<>();
            assignedGroups.add("Group1");
            assignedGroups.add("Group2");
            assignedGroups.add("Group3");
            assignedGroups.add("Group4");
            assignedGroups.forEach(assignedGroup -> {

                System.out.println("");
                System.out.println("Retrieving tickets from " + assignedGroup + " ...");
                System.out.println("");

                /**
                 * @param args
                 * Cloud Server Deployment - Create server in CIA in Active state
                 */
                // Create SOAP Response
                String qualification = "'Priority' == \"Critical\" AND 'Assigned Group' = \""+ assignedGroup + "\"";
                SOAPMessage soapResponse = null;
                try {
                    soapResponse = soapConnection.call(createSOAPRequest(qualification), url);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    if (soapResponse != null) {
                        soapResponse.writeTo(System.out);
                        System.out.println("Giant Spaghetti Monster!");
                    }
                } catch (SOAPException | IOException e) {
                    e.printStackTrace();
                }

                // Print the SOAP Response
                try {
                    printSOAPResponse(soapResponse, soapConnection);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            });

有时我在尝试检索SOAP响应时会收到NullPointerException:

<?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><soapenv:Fault><faultcode>soapenv:Server.userException</faultcode><faultstring>ERROR (302): Entry does not exist in database; </faultstring><detail><ns1:hostname xmlns:ns1="http://xml.apache.org/axis/">WEBDKBA866</ns1:hostname></detail></soapenv:Fault></soapenv:Body></soapenv:Envelope>
java.lang.NullPointerException
    at src.RemedySOAPWebService.printSOAPResponse(RemedySOAPWebService.java:218)
    at src.RemedySOAPWebService.lambda$main$0(RemedySOAPWebService.java:78)
    at java.util.ArrayList.forEach(ArrayList.java:1249)
    at src.RemedySOAPWebService.main(RemedySOAPWebService.java:49)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

现在,有没有办法忽略它并转移到正在迭代的列表中的下一个组,而不是修复异常?

例如,NPE发生在Group3。我可以转到Group4,而不是让我的脚本崩溃吗?我打印了Giant Spaghetti Monster NPE发生的地方。这可能吗?

1 个答案:

答案 0 :(得分:1)

您正在捕获SOAPException | IOException异常。赶上例外......