function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Adi85Adi85 

How to handle exceptions/error in apex webservice

Hi All,

 

I have written a sample webservice and i want to handle the excepions in my webservice. I have writen my code in betweern try-catch and catching the exceptions (if Any).

 

But i have seen "Fault String" tags in enterprise wsdl file which am not able to see in my custon wsdl file. So can anyone please let me know how to handle exception or errors in custom wsdl file (other than try-catch).

 

Here is the sample code which i have written.

 

global class OrderProcessingService {
    global class Messages{
        webservice String SuccessMessage;
        webservice String ErrorMessage;
        webservice String StatusCode;
        webservice String Identifier;
        webservice Integer LineNumber;
        webservice String StackTrace;
        webservice String ExceptionType;
    }
    global class insertRecordService{
        webservice String firstName;
        webservice String lastname;
        webservice String country;
    }
    webservice static Messages insertRecord(insertRecordService irsObj){
        Messages messageObj=new Messages();
        try{
            SampleObject001__c obj=new SampleObject001__c();
            //obj.First_Name__c=irsObj.firstName;
            //obj.Last_Name__c=irsObj.lastname;
            //obj.Country__c=irsObj.country;
            //insert obj;
            Database.Saveresult saveObj=Database.insert(obj);
            if(saveObj.isSuccess()){
                messageObj.SuccessMessage='Inserted Successfully';
            }
        }catch(System.DmlException e){
                messageObj.Identifier='-----In Side Catch------';
                messageObj.ErrorMessage=e.getMessage();
                messageObj.LineNumber=e.getLineNumber();
                messageObj.StackTrace=e.getStackTraceString();
                messageObj.ExceptionType=e.getTypeName();
              }
              catch(System.NullPointerException e){
                  messageObj.Identifier='-----In Side Catch---NullPointerException---';
                messageObj.ErrorMessage=e.getMessage();
              }
            return messageObj;
    }
}

 

Note :  Inorder to get the exception i have commented some lines.

 

Please provide me some info regarding this.

 

Thanks and Regards,

 

Adi.