Forum Discussion

Sjakie's avatar
Sjakie
Occasional Contributor
10 years ago

Exception is not caught

Hello,

 

As I try to send a message to my System Under Test, I get a HttpHostConnectException when the system is not availble. I would like to catch the exception and handle it properly. Unfortunatly somehow this does not work. I use SoapUI Pro 5.1.2 on a Windows 7 Pro machine.

 

If use the following code:

public String sendMessage(String strAddress, String strMessageToSend) {
    //variables
    String strMessage;
    StringToObjectMap map = new StringToObjectMap()

    //preparation
    map.put("address", java.net.URLDecoder.decode(strAddress, "UTF-8"));    //retrieve url to send to
    
    //Send message
    WsdlTestSuitePro tsWsdlTSP = context.getMockService().getProject().getTestSuiteByName("TestSuite");
    WsdlTestCasePro tcWsdlTCP = tsWsdlTSP.getTestCaseByName("TestCase");
    WsdlTestRequestStep trWSDLtrs = (WsdlTestRequestStep) tcWsdlTCP.getTestStepsOfType(WsdlTestRequestStep.class).get(0);
    //replace message with one that is given
    trWSDLtrs.getTestRequest().setRequestContent(strMessageToSend);
    //add the desired address
    trWSDLtrs.testRequest.setEndpoint(strAddress);
    
    //def runner = testcase.run(map, true);
    TestCaseRunner runner = null;
    try {
        runner = tcWsdlTCP.run(map, false); //wait for an answer
        log.info("Sim: Message sent to Consumer");
    }
//    catch(HttpHostConnectException hhce) {
//        //hhce.printStackTrace();
//        log.error("hhce: " + hhce.getMessage() );
//        strMessage = hhce.getMessage();
//    }
    catch(Exception e) {
        //e.printStackTrace();
        log.error("Exception: " + e.getMessage() );
        strMessage = e.getMessage();
    }

    log.info(trWSDLtrs.RESPONSE_AS_XML);

    if (runner == null ) {
        strMessage = "An error has occured: " +strMessage
    }
    else if (runner.getStatus().toString().equals("FINISHED") ){
        strMessage = "OK"
    }
    else {
        strMessage = runner.getReason();
    }
    
    return strMessage;
}

 

My logging prints the line "Sim: Message sent to Consumer". The error logging shows the complete stacktrace, The stacktrace refers to the line "runner = tcWsdlTCP.run(map, false); //wait for an answer".

 

Why is the exception not caught? Is there any other way of doing this? I just want to return the message of the HttpHostConnectException.

 

Thanks in advance!

4 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3
    Are you sure if it is HttpHostConnectException? Or it java.net.UnknownHostException ?
    • Sjakie's avatar
      Sjakie
      Occasional Contributor

      Yes, because that is what is in the error log where the StackTrace is shown.

       

      To be sure, I had added an extra catch-part for Exception as can be seen in the code.

      • nmrao's avatar
        nmrao
        Champion Level 3
        Would like to catch the exception in this order UnknownHostException, HttpHostConnectException and then Exception to see if that helps?