public pagereference savePassword()
    if((objAccountTemp.Contact_Name_1__c !=  null && objAccountTemp.Contact_Name_1__c !=  '' && objAccountTemp.Affiliate_User_Name__c != null && objAccountTemp.Affiliate_User_Name__c != '')){
        if(objAccountTemp.Affiliate_User_Name__c == objAccountTemp.Contact_Name_1__c){
            If(LoginType == 'affiliate') 
                    String s = objAccountTemp.Contact_Name_1__c;
                    Account acc = new Account();
                    acc=[select Affiliate_Password__c from Account where id=:Affid limit 1];
                    acc.Affiliate_Password__c = s;
                    update acc;
                    changePasswordMsg = true;
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Your password changed successfully!'));
                    IsEditMode = false;
                    IsSaveMode = true;
                    changePass = false; 
                    lstAcc[0].Affiliate_Password__c = s; 
                    changePassErrormsg = false;     
                    return null;
                    }catch(exception e){
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Error Found!'));
                    return null;
  • 1
    try updating an Account record which doesn't exist. So it'll enter the catch block with DMLException
    – Poonam
    Apr 13, 2015 at 6:06
  • Not sure why you have hardcoded zero for lstAcc. As a best practice, you would want to do a null and empty check on the list unless you are really sure that it would not be null and empty. We dont see how lstAcc is initialized in the code that you have shared. Try a scenario where lstAcc does not get any records and you code will definitely break and enter the catch block. Apr 13, 2015 at 6:20
  • Can we see the test code you've already written ? You haven't really explained why you have a hard time covering this in your test code, which sort of means you haven't really asked a question. Apr 13, 2015 at 7:46

2 Answers 2


Please use below snippet in try block:

  Integer i = 10/0;

Extract the code into a separate method and write tests for that.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .