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 softwaredevelopment@company.com."


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

    //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.

  • 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 Jan 14 '16 at 1:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.