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
ponneri navadeepponneri navadeep 

how to cover try and catch blocks in test classes?

mritzimritzi
An easy way out to cover try catch block is this:
 
try{
    //do something
    if(Test.isRunningTest())
        Integer i = 9/0;
}catch(Exception e){
    // something here
}
This way try/cach blovk would get covered in one shot.

Other not so easy way is to design your data/queries in a way that try block executes in some cases while it fails in other cases, that way try/catch blocks would get covered.

Mark this as Best Answer, if this helps solve your problem.