4

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.

CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
mathlearner
  • 7,509
  • 31
  • 126
  • 189

2 Answers2

2

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

Pavel Slepiankou
  • 3,516
  • 2
  • 25
  • 30
0

You can use a try statement in your controller:

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'));
} 

Then in the visualForce page you write apex:messages/ where you want the message to be displayed.

user16217248
  • 3,119
  • 19
  • 19
  • 37
Levementum
  • 11
  • 2