Custom Exception Message in apex Salesforce

apexapex-codesalesforcevisualforce

I want to create a custom exception. In this exception I want to make a constructor which take one string arguments and append in getMessage .How to accomplish this.

 public class TimelineException extends Exception{
  private String message;
    public override String getMessage(){
      return 'Not able to post to Timeline.getting response from Api : '+ message;
    }
  }

Main problem I am facing is when I use this:

public TimelineException(Sting x){
}

then its give me error

Save error: System exception constructor already defined: (String) GMirror.cls /Google Salesforce Integration/src/classes.

Best Answer

You do not need to implement something. Just create a your exception class which would extends salesforce Exception class.

public class CommonException extends Exception {}

then just use it as other exception classes

try {
    throw new CommonException('Test Common Exception');
} catch(CommonException ex) {
    System.debug(ex.getMessage());        
}

console output:

09:18:55:059 USER_DEBUG [4]|DEBUG|Test Common Exception

Related Topic