Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (11.9k points)

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;

    }

  }

The main problem I am facing is when I use this:

public TimelineException(Sting x){
}
then its give me an error:
Save error: System exception constructor already defined: (String) GMirror.cls /Google Salesforce Integration/src/classes.

1 Answer

0 votes
by (32.1k points)

Create your exception class which would extend salesforce Exception class.

public class CommonException extends Exception {}

Use this as an exception class

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

To learn in-depth about Workflow in Salesforce, sign up for an industry based Salesforce Training

Browse Categories

...