[SalesForce] Statements after the catch block still got executed after an exception occured. Is that normal

I am making a callout to SAP webservice in order to create a customer in SAP. The customer data is nothing but our Account. Now i am getting an exception "IO Exception: Read timed out". i knew that increasing the timeout will solve the issue. But statements inside and after the catch block got executed even after an exception occurred. Is that normal? I need the statements inside the catch block should executed when an exception occurred and the code execution should get stopped. Why this is happening? Is this normal behavior of try catch? Any ideas please…

    public static void CreateCustomer(String accid) {
    //Variable declaration
    CustomerCreateRequestDTO cust = new CustomerCreateRequestDTO();
    sapComDocumentSapSoapFunctionsMcS.Z_GSSMWFM_HNDL_EVNTRQST00 sap1 = new sapComDocumentSapSoapFunctionsMcS.Z_GSSMWFM_HNDL_EVNTRQST00();       
    Map<List<String>,List<List<String>>> mapSAP = new Map<List<String>,List<List<String>>> {};
    List<String> strReqData = new List<String>{};

    sap1.timeout_x = 900;
    sapComDocumentSapSoapFunctionsMcS.ZgssmbstDatarcrd01 req = new sapComDocumentSapSoapFunctionsMcS.ZgssmbstDatarcrd01();

    Account acc = [select Account_Group__c, Title__c, First_Name__c, Last_Name__c, House_No__c, Search_Term__c, BillingStreet, BillingCountry, BillingPostalCode, SAP_REGIO__c, BillingCity from Account where Id =: accid];        

    cust.AccountGroup = acc.Account_Group__c;
    cust.Title = acc.Title__c;
    cust.FirstName = acc.First_Name__c;
    cust.LastName = acc.Last_Name__c;
    cust.SearchTerm = acc.Search_Term__c;
    cust.StreetHNo = acc.BillingStreet;
    cust.PostCode = acc.BillingPostalCode;
    cust.Region = acc.SAP_REGIO__c;
    cust.City = acc.BillingCity;
    cust.Country = acc.BillingCountry;

    //CUSTOMER CREATION
    /*
    //CUSTOMER CREATION
    DEVICE-ID:160000000000000:DEVICE-TYPE:SFDC-CLIENT:APPLICATION-ID:SFDC-SALES
    NOTATION:ZML:VERSION:0:DELIMITER:[.]
    EVENT[.]SFDC-CUST-CREATE[.]VERSION[.]0[.]RESPONSE-TYPE[.]FULL-SETS
    DATA-TYPE[.]ZGSEVAST_SDCRTCUST[.]ANRED[.]LAND1[.]NAME1[.]NAME2[.]ORT01[.]PSTLZ[.]REGIO[.]SORTL[.]STRAS[.]MCOD1[.]KTOKD
    ZGSEVAST_SDCRTCUST[.]Company[.]US[.]EMAX[.]ANAHEIM[.]ANA9[.]0023[.]CA[.]AB[.]AB[.]AB[.]ZARG

    Account Group   KTOKD
    First Name  NAME1
    Last Name   NAME2
    Title       ANRED   
    Search Term SORTL
    Street/H No.    STRAS

    Postal Code:    PSTLZ
    City:   ORT01
    Region: REGIO
    Country LAND1

    */

    String EventType = 'EVENT[.]SFDC-CUST-CREATE[.]VERSION[.]0[.]RESPONSE-TYPE[.]FULL-SETS';
    String InputFields = 'DATA-TYPE[.]ZGSEVAST_SDCRTCUST[.]ANRED[.]LAND1[.]NAME1[.]NAME2[.]ORT01[.]PSTLZ[.]REGIO[.]SORTL[.]STRAS[.]KTOKD';
    String InputData = 'ZGSEVAST_SDCRTCUST[.]'+cust.Title+'[.]'+cust.Country+'[.]'+cust.FirstName+'[.]'+cust.LastName+'[.]'+cust.City+'[.]'+cust.PostCode+'[.]'+cust.Region+'[.]'+cust.SearchTerm+'[.]'+cust.StreetHNo+'[.]'+cust.AccountGroup;
    strReqData.add(EventType);
    strReqData.add(InputFields);
    strReqData.add(InputData);
    req.item = Utility.LoadRequestData(strReqData);
    sapComDocumentSapSoapFunctionsMcS.ZGssmwfmHndlEvntrqst00Response_element ret;
    try {
        ret =  sap1.ZGssmwfmHndlEvntrqst00('',req);
    } catch(Exception ex) {
        System.debug('>>>>>>>>>>>>>>>>>>>>'+ex.getmessage());
        ProcessorControl.inFutureContext = true;
        Account ac = [select id, SAP_KUNNR__c from account where id =: AccId];
        ac.SAP_KUNNR__c = 'xxxxxxxxxx';//r.MessageV2;
        update ac;
    }
        System.debug('$$$$$$$$$$$$$$$$$$$$$$$$$'+ret);
        sapComDocumentSapSoapFunctionsMcS.Bapiret2 r = ret.DpostMssg.item[0];
    ProcessorControl.inFutureContext = true;
    Account ac = [select id, SAP_KUNNR__c from account where id =: AccId];
    ac.SAP_KUNNR__c = r.MessageV2;
    update ac;


} 

Thanks,
Baskaran

Best Answer

This is indeed normal behaviour.

If you need code execution to stop in the catch block, put a return; statement at the end of that block, or, if that is applicable, don't catch the exception at all.

Related Topic