Print this page

Apex Custom Exception messages

Knowledge Article Number 000050990
Description Salesforce is appending an exception runtime message to the exception thrown when using the <apex:message> /<apex:messages>  tag.

This exception message is added even after you have caught and thrown your own custom exception class.

 
Resolution
This would be working as expected. Taking the following code snippet:
try{ 
   testField = value; 
   // This will provoke an error when you type an string 
   Decimal testDec = Decimal.valueOf(value);
}catch (Exception e){ 
   throw new MyCustomException('Test Custom Exception.'); 
}
Decimal testDec = Decimal.valueOf(value); will throw a TypeException and the code will enter the catch block. 
In the catch block you are throwing a new exception, this is not caught and hence the runtime exception message will be displayed along with your custom message. common.apex.runtime.impl.ExecutionException is coming from the base Exception class. 

Possible workaround #1 (this depends on if you require field level exception handling or not, not in this case)

If you used the following code then only your message will be displayed (using apex:messages instead of apex:message) 
try{ 
   testField = value; 
   // This will provoke an error when you type an string 
   Decimal testDec = Decimal.valueOf(value);
}catch (Exception e){ 
   ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'my error msg'); 
   ApexPages.addMessage(myMsg); 
}

Possible workaround #2 (if field level is required, can't really recommend this, just to show that we can always modify the DOM if you're not happy with what SFDC generates)

You could use Javascript to modify the html outputted. 
You could do something like this: 
var errorString = document.getElementsByClassName("testError") 
var modErrorString = errorString[0]; 
modErrorString = modErrorString.innerText; 
modErrorString.slice(modErrorString.lastIndexOf(":") + 1);
*getElementsByClassName is apart of HTML5




promote demote