apex Salesforce中的自定义异常消息

我想创建一个自定义异常.在这个例外中,我想创建一个构造函数,它接受一个字符串参数并附加在getMessage中.如何实现这一点.

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

我面临的主要问题是当我使用它时:

public TimelineException(Sting x){
}

然后它给我错误

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

最佳答案 你不需要实现某些东西.只需创建一个可以扩展salesforce Exception类的异常类.

public class CommonException extends Exception {}

然后将其用作其他异常类

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

控制台输出:

09:18:55:059 USER_DEBUG [4] | DEBUG |测试常见异常

点赞