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
Suresh RaghuramSuresh Raghuram 

how to catch particular exception

when there are many dml statements and if the error or exception occoured , how could we catch that particular exception and show to the user.

 

Any code snippet or suggestion is a lot appricated.

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippet as reference:

try{

     update account;

} catch (DMLException e){

     ApexPages.addMessages(e);

 

     Messaging.SingleEmailMessagemail=new Messaging.SingleEmailMessage();

     String[] toAddresses = new String[] {'developer@acme.com'};

     mail.setToAddresses(toAddresses);

     mail.setReplyTo('developer@acme.com');

     mail.setSenderDisplayName('Apex error message');

     mail.setSubject('Error from Org : ' + UserInfo.getOrganizationName());

     mail.setPlainTextBody(e.getMessage());

     Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

}

For more detail follow the below link:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_exception_methods.htm

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.