Martin Erlic
Martin Erlic

Reputation: 5667

Continue with list iteration after exception

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

            });

Sometimes I get a NullPointerException while attempting to retrieve a SOAP response:

<?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)

Now, instead of fixing the exception, is there a way to ignore it and move on to the next group in the list that is being iterated over?

For example, the NPE occurs at Group3. Instead of having my script crash, can I just move to Group4? I've printed Giant Spaghetti Monster where the NPE occurs. Is this possible?

Upvotes: 0

Views: 317

Answers (1)

Anwarul Islam
Anwarul Islam

Reputation: 141

You are catching SOAPException | IOException. catch the Exception too..

Upvotes: 1

Related Questions