Salesforce Platform Developer I (legacy) — Question 12
A developer wrote a unit test to confirm that a custom exception works properly in a custom controller, but the test failed due to an exception being thrown.
Which step should the developer take to resolve the issue and properly test the exception?
Answer options
- A. Use try/catch within the unit test to catch the exception.
- B. Use the finally bloc within the unit test to populate the exception.
- C. Use the database methods with all or none set to FALSE.
- D. Use Test.isRunningTest() within the custom controller.
Correct answer: A
Explanation
The correct answer is A because using try/catch allows the developer to handle the exception that is being thrown during the test, enabling proper verification of the custom exception's behavior. The other options do not address the root cause of the test failure effectively; for example, option B's finally block does not catch exceptions, option C is unrelated to exception handling, and option D does not resolve the testing issue directly.