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
Gwirizanani SinoiaGwirizanani Sinoia 

try catch block code coverage

Help
Trying to get code coverage for the code below. I am currently getting 69% on this triggger. I can not seem to cover  the try cacth block. Please help
Thanks

trigger ContactTrigger on Contact (before update, after update) {
    Contacthandler = ContactHandler.getHandler();
    
    try {        
        if(Trigger.isBefore) {
            if (Trigger.isUpdate) {
                
                
                handler.checkmytest(Trigger.new);//check if dates have changed
                handler.checkpeople(Trigger.new);//sort dates according to source and priority
                
        }

        
        if(Trigger.isAfter) {
            if (Trigger.isUpdate) {
                handler.individual(Trigger.new, Trigger.oldMap);
            }
        }
    }
    catch(DMLException e) {
        for (Integer i=0 ; i<e.getNumDml() ; i++) {
            Contact o = Trigger.newMap.get(e.getDmlId(i));
            o.addError(e.getDmlMessage(i));
        }
    }
}

 
Best Answer chosen by Gwirizanani Sinoia
Jake Hebert 22Jake Hebert 22

Hey there!

I would argue that there is no reason for this to be wrapped in a Try/catch block.

1) Wrapping your whole trigger in a "Try" is not best practice. Errors are ok, especially in a trigger when you need immediate feedback from the database that something failed. We don't want things to fail invisibly, so if a trigger fails, let it fail. The try/catch block in apex is more useful in a custom interface when you need to manage the user feedback yourself in your own UI (via a lightning compontent or visualforce for example)

2) This catch block is basically doing the exact same thing that the trigger would do if it failed anyway. All you are doing is adding the DML error to the contact, which is exactly what the DML failure does in a trigger.

So, take out the try/catch from your trigger and everything should work exactly the same way. As far as text coverage, you would have to cause something to fail the "Try" in order to test the "Catch". Take out the try catch and bingo, you're covered!

All Answers

Raj VakatiRaj Vakati
Try this 
 
@isTest
private class TestCacth {
  static testMethod void testCacth() {    
 try{
	Contact c = new Contact() ; 
c.LastName ='Test';
e.Email='test@gmail.com' ; 

insert c ; 

c.LastName ='';
update c ;	
	 
	 
 }catch(Exception e){
 }
  }
}

 
Jake Hebert 22Jake Hebert 22

Hey there!

I would argue that there is no reason for this to be wrapped in a Try/catch block.

1) Wrapping your whole trigger in a "Try" is not best practice. Errors are ok, especially in a trigger when you need immediate feedback from the database that something failed. We don't want things to fail invisibly, so if a trigger fails, let it fail. The try/catch block in apex is more useful in a custom interface when you need to manage the user feedback yourself in your own UI (via a lightning compontent or visualforce for example)

2) This catch block is basically doing the exact same thing that the trigger would do if it failed anyway. All you are doing is adding the DML error to the contact, which is exactly what the DML failure does in a trigger.

So, take out the try/catch from your trigger and everything should work exactly the same way. As far as text coverage, you would have to cause something to fail the "Try" in order to test the "Catch". Take out the try catch and bingo, you're covered!

This was selected as the best answer
Gwirizanani SinoiaGwirizanani Sinoia
Hi @ Raj Vakati sorry its does not work.
@Jake Hebert 22 I will try your approach and see.

Thanks