function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Avoiding the 'Script Thrown Exception' error message

Hi all,
I have a problem I've not seen until recently.
When I hit save, and throw my own exception: I'm getting a message added to the page: 'Script Thrown Exception'.
This is not a message in the exception that I have made. In addition: it's also adding the desired message from my controller.

Here is what it looks like:
User-added image

The controller code for the save function is as follows:
public void Save(){
    savepoint sp = Database.setSavePoint();
    try {        
        for(AutoNumber__c r : AutoNumberRecords){
            if(r.Length__c == null || r.Length__c < 1){
                CODException e = new CODException(new MessageCodes.MessageCode('AN900','The Length must be greater than 0'));
                throw e;
            if(r.CurrentNumber__c == null || r.CurrentNumber__c < 1){
                CODException e = new CODException(new MessageCodes.MessageCode('AN901','The Length must be greater than 0'));
                throw e;
        Update AutoNumberRecords;
        NextRecordNames = null;
    } catch (exception e){

COD Exception is a custom exception class I've made:
public class CODException extends Exception{

private MessageCodes.MessageCode MessageCode{get;set;}

//The additional messages stores a list of any information to be appended to an error
//    These could be from DML Exceptions, or multiple errors
private list<string> AdditionalMessages{
    private set;

//Returns the COD error code
public string GetCode(){
    return  MessageCode != null && MessageCode.Code != null ? MessageCode.Code.toUpperCase() : null;         
//Componse a message from what data we have
private string GetCODMessage(){
    string s = (MessageCode != null && MessageCode.Code != null ? MessageCode.Code : '') + (MessageCode != null && MessageCode.Message != null ? ' - ' + MessageCode.Message: '');
    if(AdditionalMessages != null && AdditionalMessages.size() > 0) {   
        for(string am : AdditionalMessages){
            s += '<br/>&bull; ' + am;
    return s;

//The overriden GetMessage method from the standard Exception class
//This is designed to show all the additional information that our exception
//    method has in it. It may be augemented over time
//I am unaware of how the 'escaping' will work when throwing this, or when using a
//    standard sObejct.AddError() method with the escape signature passed in   
public override string GetMessage(){
    return GetCODMessage();

//With a MessageCodes.MessageCode and a list of string
public CODException(MessageCodes.MessageCode pMessageCode, string[] pAdditionalMessages) {
    MessageCode = pMessageCode;
    AdditionalMessages = pAdditionalMessages;
//With a MessageCodes.MessageCode and a set of string
public CODException(MessageCodes.MessageCode pMessageCode, set<string> pAdditionalMessages) {
    this(pMessageCode, new list<string>(pAdditionalMessages));
//With a MessageCodes.MessageCode and a string
public CODException(MessageCodes.MessageCode pMessageCode, string pString) {
    this(pMessageCode,new string[]{pString});
//With a MessageCodes.MessageCode
public CODException(MessageCodes.MessageCode pMessageCode) {
    MessageCode = pMessageCode;
//With a MessageCodes.Message code and a DML Exception
public CODException(Messagecodes.MessageCode pMessageCode, DMLException pDMLx){
    MessageCode = pMessageCode;
    set<string> s = new set<string>();
    for(integer i = 0; i < pDMLx.getNumDML(); i++){
    AdditionalMessages = new list<string>(s);


As well as MessageCodes
public class MessageCodes{
////////// ERROR_AND_MESSAGE_CODES //////////
//A class used to hold all Message codes related to a particular area of the system
//    Populated by a map of code to message, and retrieved as a case insensitive map.Get() type
//    of method. 
//This used to be a listing of enum error codes, but it was discovered that enums will only permit 100 entries
//    so I deprecated that to this

//The class that hold all message codes related to a particular area of the system
public MessageCodes.MessageCode Get(string pCode) {
    MessageCode m;
    if(pCode != null && pCode.DeleteWhiteSpace() != '' && MessageCodeMap.get(pCode.ToUpperCase().trim()) != null) {
        m = new MessageCodes.MessageCode(pCode.ToUpperCase().trim(),MessageCodeMap.get(pCode.ToUpperCase().trim()));
    return m;

private map<string,string> MessageCodeMap {
    get {
        if(MessageCodeMap == null) {
            MessageCodeMap = new map<string,string>();
        return MessageCodeMap;
    } private set;

//Pass in a map of error code to code message
public MessageCodes(map<string,string> pCodeMessages){
    for(string s : pCodeMessages.KeySet()){

//What is returned after requesting an error message and code
public class MessageCode{
    public string Code {get;private set;}
    public string Message {get;private set;}
    public MessageCode(string pCode, string pMessage){
        Code = pCode.ToUpperCase().trim();
        Message = pMessage;

On top of all that, according to the documentation: if I use the sObject.AddError method it is supposed to prevent any DML from ocurring. (As referenced here: - however it did not.
If I changed the value in one of my fields to forcibly throw the exception: the DML still occurred. So I had to add the Savepoint and rollback to it... Is this right?


You just need to remove or comment below line in your code then this excpetion message will remove "Script-thrown exception".
//ApexPages.addMessages(e); In Save method 20 no. line
Ashlekh Gera