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

Test method coverage for Exceptions



I am handling exception using try-catch but at the time of writing test class fail to cover catch block part,

this happens at the time when I am inserting any object.


For example,




insert contact;


catch(Exception e) // fail to cover this part of code



Can anybody help me to solve this problem.




Best Answer chosen by Admin (Salesforce Developers) 



If the Type of Exception in Catch is "Exceltion" class then simply in your test code :

Assign the ID to object you are trying to insert.


It will give you error that object cannot have ID suring insert and your control will come into catch block.


I hope this will help you.