apex Salesforce中的自定义异常消息

时间:2013-09-24 03:42:51

标签: salesforce apex-code visualforce apex

我想创建一个自定义异常。在这个例外中,我想创建一个构造函数,它接受一个字符串参数并附加在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){
}

然后它给我错误

  

保存错误:已定义系统异常构造函数:(String)GMirror.cls / Google Salesforce Integration / src / classes。

2 个答案:

答案 0 :(得分:2)

您不需要实施某些内容。只需创建一个扩展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|Test Common Exception

答案 1 :(得分:0)

你可以在你的控制器中使用这样的东西:

try
{
    'content of what you tring to do'
}
catch(Exception e)
{
     system.debug(e.getStackTraceString());
     ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'special friendly message to user'));
} 

然后在visualForce页面中编写apex:messages /您希望显示消息的位置