You need to sign in to do that
Don't have an account?
Manvitha
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 !
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 !
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 : 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
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 : 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.
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:
- 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.
I hope this would be helpfull.✔ (https://socialfunda.in/facebook-vip-work-copy/)Note that this is just an example and you may need to modify the test code to fit your specific requirements.