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
ManvithaManvitha 

how to test the exception catch block

Hi All ,

can somebody help me on this please !?
I need to test the below piece of code where i have written this in catch block of some pqr class method
But i am strucked how to test it in anonymous window:
catch (exception ex)
mainclass.mainwrapper wrapp = new mainclass.mainwrapper();
                wrapp.excp= ex;
                wrapp.orgId = UserInfo.getOrganizationId();
                wrapp.winName = UserInfo.getOrganizationName();
                wrapp.cName = 'xyz class';
                wrapp.mName = 'abc method';
                ExceptionLog__c newExceptionLog = new ExceptionLog__c();
                newExceptionLog = mainclass.mno-method(wrapp);
                insert newExceptionLog;


Thanks in Advance !
Best Answer chosen by Manvitha
SubratSubrat (Salesforce Developers) 
Hello manvitha ,

To test the code in the catch block, In the Execute Anonymous window, you can write the code that you want to test :

For Example :
try {

    // Code that throws an exception

} catch (Exception ex) {
    mainclass.mainwrapper wrapp = new mainclass.mainwrapper();
    wrapp.excp = ex;
    wrapp.orgId = UserInfo.getOrganizationId();
    wrapp.winName = UserInfo.getOrganizationName();
    wrapp.cName = 'xyz class';
    wrapp.mName = 'abc method';
    ExceptionLog__c newExceptionLog = mainclass.mno-method(wrapp);
    insert newExceptionLog;
}
In this sample code, you can replace the comment // Code that throws an exception with the code that you want to test. When an exception is caught, the catch block will execute, and the code inside it will be executed.

Also Refer -> https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_exception_trycatch_example.htm

If the above information helps , please mark this as Best Answer.
Thank you.

All Answers

SubratSubrat (Salesforce Developers) 
Hello manvitha ,

To test the code in the catch block, In the Execute Anonymous window, you can write the code that you want to test :

For Example :
try {

    // Code that throws an exception

} catch (Exception ex) {
    mainclass.mainwrapper wrapp = new mainclass.mainwrapper();
    wrapp.excp = ex;
    wrapp.orgId = UserInfo.getOrganizationId();
    wrapp.winName = UserInfo.getOrganizationName();
    wrapp.cName = 'xyz class';
    wrapp.mName = 'abc method';
    ExceptionLog__c newExceptionLog = mainclass.mno-method(wrapp);
    insert newExceptionLog;
}
In this sample code, you can replace the comment // Code that throws an exception with the code that you want to test. When an exception is caught, the catch block will execute, and the code inside it will be executed.

Also Refer -> https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_exception_trycatch_example.htm

If the above information helps , please mark this as Best Answer.
Thank you.
This was selected as the best answer
Joe L. ThompsonJoe L. Thompson
To test the exception catch block in your code, you can simulate an exception by deliberately causing an error in the code. Here's an example of how you can do that:
  • Modify the code inside the method where you have the catch block to intentionally throw an exception. For example, you can add a line of code that throws a custom exception:
throw new CustomException('Testing exception handling');

  • Write a test method that calls the method with the modified code. Make sure to call the method in a way that triggers the exception. For example:
@IsTest
static void testExceptionHandling() {
    try {
        // Call the method with the modified code
        MyClass.myMethod();
        // If the code reaches here, the test has failed because the exception was not caught
        System.assert(false, 'Expected exception was not thrown');
    } catch (CustomException ex) {
        // Verify that the exception was caught and logged correctly
        System.assertEquals('Testing exception handling', ex.getMessage());
        // Verify that the exception was logged in the database
        List<ExceptionLog__c> logs = [SELECT Id FROM ExceptionLog__c];
        System.assertEquals(1, logs.size());
        ExceptionLog__c log = logs[0];
        System.assertEquals(UserInfo.getOrganizationId(), log.OrganizationId__c);
        System.assertEquals(UserInfo.getOrganizationName(), log.WindowName__c);
        System.assertEquals('xyz class', log.ClassName__c);
        System.assertEquals('abc method', log.MethodName__c);
    }
}
  • In this example, the test method calls MyClass.myMethod() which throws a CustomException. The catch block in the method logs the exception to the database, and the test method verifies that the exception was caught and logged correctly. If the exception is not caught, the test will fail because the System.assert(false) line is executed.
    Note that this is just an example and you may need to modify the test code to fit your specific requirements.
I hope this would be helpfull.✔ (https://socialfunda.in/facebook-vip-work-copy/)