我想创建一个自定义异常。在这个例外中,我想制作一个构造函数,它接受一个字符串参数并附加在
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){
}
然后它给我错误
保存错误:系统异常构造函数已定义:(字符串)GMirror.cls /Google Salesforce Integration/src/classes.
您不需要实施某些东西。只需创建一个扩展 salesforce 异常类的异常类。
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
您可以在控制器中使用
try
语句:
try
{
// What you are trying 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/
你想要显示消息的地方。