I am using

 catch (Exception e){ 
   ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'my error msg'); 

And have <apex:pageMessages/> in my VF page.

still i get my custom message + the salesforce standard exception displayed at my page. I want only my message to be shown.

For comment

catch(Exception e){
     if(validateVariable.contains('STRING_TOO_LONG') &&
          validateVariable.contains('data value too large') ) { 
        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'Character limit exceeded. Please limit your essay in the prescribed character limit'); 
  • can you add your dml statement code. Is it inside try or not? one more thing sounds like you are using 2 <apex:pageMessages/> in your VF page – Ratan Paul Dec 11 '15 at 14:02
  • Its a text field , i have put some character limit on it . And i want my custom error message should be thrown instead of the standard salesforce one. – user27072 Dec 11 '15 at 14:05
  • how many <apex:pageMessages/> tag you are using in your page? – Ratan Paul Dec 11 '15 at 14:05
  • Only 1 <apex:pageMessages/> is there – user27072 Dec 11 '15 at 14:06
  • Could you also include the part in your code where the error is occurring. – martin Dec 11 '15 at 14:07

The appropriate way to handle this is by creating a custom exception that gets thrown as an inner exception using a utility class. You first catch the exception, then rethrow it, catching it with your custom "inner exception" that produces the error message you'd like to send. Here's an example from the documentation:

public class MerchandiseException extends Exception {}

public class MerchandiseUtility {
    public static void mainProcessing() {
        try {
        } catch(MerchandiseException me) {
            System.debug('Message: ' + me.getMessage());    
            System.debug('Cause: ' + me.getCause());    
            System.debug('Line number: ' + me.getLineNumber());    
            System.debug('Stack trace: ' + me.getStackTraceString());    

    public static void insertMerchandise() {
        try {
            // Insert merchandise without required fields
            Merchandise__c m = new Merchandise__c();
            insert m;
        } catch(DmlException e) {
            // Something happened that prevents the insertion
            // of Employee custom objects, so throw a more
            // specific exception.
            throw new MerchandiseException(
                'Merchandise item could not be inserted.', e);

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.