In Apex, the Try-Catch method is used to handle exceptions that may occur during the execution of a block of code. It allows you to catch and handle specific types of exceptions, preventing your code from crashing and providing error handling capabilities.
If you try to execute the code example as follows without a try-catch block, and the index is out of bounds (as in the example where you attempt to access an element at index 10 in a list with only 5 elements), it will result in an unhandled exception, and your code will likely fail to execute.
// Define a list
List<Integer> numberList = new List<Integer>{1, 2, 3, 4, 5};
System.debug(‘Element at index 10: ‘ + element);
If you run this code, it will throw an error at runtime, and your debug logs or console will show an error message indicating the issue. Using try-catch allows you to catch and handle such exceptions, preventing them from causing the entire program to fail and giving you the opportunity to take appropriate actions when an exceptional situation occurs.
To handle this issue you can adjust the catch block like this:
// Define a list
List<Integer> numberList = new List<Integer>{1, 2, 3, 4, 5};
try {
System.debug(‘Element at index 10: ‘ + element);
} catch (Exception ex) {
System.debug(‘Exception caught: ‘ + ex.getMessage());
}
In this example, the numberList contains elements from index 0 to 5. However, we attempt to access an element at index 10, which is out of bounds. This will result in an error, and the try-catch block is used to catch and handle this exception.
This demonstrates how a try-catch block can be employed to gracefully handle situations where an index might be out of bounds in a list, preventing the program from failing due to an unhandled exception.
There are several ways to create test data in Apex test classes, including creating test data directly in the test class, using a factory class, or uploading test data into a static resource. The choice of method depends on the complexity of your test data requirements and your preferences for test data generation.
You can create test records directly within your test method as we´ve already done in our first test example for our TestContactTrigger class. This approach is suitable for simple test cases where you only need a few records to test a specific piece of functionality.
A factory class is a separate Apex class dedicated to creating test data. It’s especially useful when you have complex data structures or need to create a large number of related records. It used to centralize and standardize the creation of test data, making it easier to maintain and reuse test data creation logic across multiple test classes with reusable methods for creating test records. It involves creating a public test class that is excluded from the organization code size limit and executes in a test context. To create a test data factory in Salesforce, you can follow the Test Data Factory pattern including creation of a public test class with the @isTest annotation and define static methods within the test class to create various types of test data.