I have written a test class to cover a trigger and got 80% code coverage but am unable to cover try catch block in the trigger.

  • 4
    Yes please, post your relevant code in the question. Commented Feb 1, 2017 at 11:13
  • 1
    Make sure code throws exception in one use case so it covers the catch block. Commented Feb 1, 2017 at 11:14
  • 2
    post your trigger and helper class Commented Feb 1, 2017 at 11:18
  • @Laxman kindly don't use regards, thanks in your question or answer posts. Secondly, do post your code, we won't be able to help you appropriately without looking at what you have tried so far.
    – Mahmood
    Commented Mar 6, 2017 at 8:09

1 Answer 1


To get the catch block to execute, an exception would need to originate from the try block of code in the trigger. If your trigger was doing an update, some validation elsewhere might result in a DMLException being thrown. The important thing about your test methods is that they verify your code works correctly. Unless you have a way of crafting a record that will fail (1st suggestion above), the only other method which is strongly not recommended-- would be to write a exception built into the actual code you're testing. This involves using Test.isRunningTest to set up an arbitrary failure.

For reference, I am also providing you the following link that will help you:


You must log in to answer this question.

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