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
jucuzoglujucuzoglu 

Throwing a standard exception with a particular message

I use a try/catch block with code similar to the following to display a variety of exceptions including validation errors. Shown below is the catch poriton:

 

        catch(Exception e)
        {
        	ApexPages.addMessages(e);
        }

 Now I would like to THROW a standard exception. i.e.

 

Try {

(Validate some condition)

}

catch (DMLException DMLe){

[How do I place code here that will provide a specific static message that i want to show as the particular exception message]

}

Best Answer chosen by Admin (Salesforce Developers) 
gm_sfdc_powerdegm_sfdc_powerde

I don't think you can construct instances of standard Apex exceptions.  You will have to wrap your message in a custom exception created by extending standard exception.

All Answers

gm_sfdc_powerdegm_sfdc_powerde

I don't think you can construct instances of standard Apex exceptions.  You will have to wrap your message in a custom exception created by extending standard exception.

This was selected as the best answer
Andy BoettcherAndy Boettcher

If you are trying to push back a very specific / static message, you could have the different CATCH statements for different exceptions and do something like this:

 

try {
// Logic
} catch(exception e) {
ApexPages.addMessage(New ApexPages.Message(ApexPages.Severity.FATAL, 'whatever message you want'));
}

 

Is that what you're looking for?  Or are you looking to throw your own custom rolled exception?

 

-Andy

Chamil MadusankaChamil Madusanka

refer : http://boards.developerforce.com/t5/Visualforce-Development/How-do-I-properly-catch-and-display-error-messages-in/m-p/374773#M44448

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

jucuzoglujucuzoglu

Custom extension was the way to go.

 

I created a custom exception class similar to this article which is the easiest I have read on the topic: http://th3silverlining.com/2009/06/11/throwing-custom-apex-exceptions/

 

Then after creating a custom extension I simply inserted a bunch of IF statements and inside used the code below

throw new customException('My Text Here');

 

Worked well. Thanks to everyone for helping out.

SFDC newSFDC new
==>we can now throw Standard Exception with particular message.
public static void insertAccount() {
        try {
             //Account a=new Account();
            //insert a;
            throw new DmlException('Sorry insertion fails ');
        } catch(DmlException e) {
            System.debug('thrown standard DML exception '+e);
        }
    }
}
==>We can now extend exception class
refer: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_exception_custom.htm

Mark this as best answer if it helps.