1

Currently when Apex code on a VisualForce page throws an exception the user is shown what line of code threw the exception and some other information. I want to change this to show a custom error message, something along the lines of "Something went wrong, please contact Software Development at [email protected]."

1 Answer 1

1

Have you ever tried exception handling via Try-Catch block?

Put your code inside a try block and catch block with catch the exception if any exception occurs. You can add the custom exception message to apexPages variable

try{
    //Your code goes here
}Catch(Exception e){
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Your custom message'));
    //You can also access exception string by e.getMessage() method
}

More details about exception class - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_exception_methods.htm

Modify your VF Page to include a tag and it will display the error when added by your apex class.

1
  • yep - totally agree -- and if I were in "Software Development" @ company.com, and this was an otherwise unexpected, albeit caught, exception, I'd like the ApexPages.Message to include the value of e.getStackTraceString() so I might be able to diagnose/fix the problem faster rather than having to force the user to reproduce the problem with debug logs on.
    – cropredy
    Commented Jan 14, 2016 at 1:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .