Lazy objects are immutable by design. This means that they always return the same instance that they were initialized with. We have seen that we can pass initialization logic to Lazy<T> and that we can have initialization logic in the underlying object's constructor. What will happen if the construction/initialization logic is faulty and throws an exception? The behavior of Lazy<T> in this scenario depends on the value of the LazyThreadSafetyMode enumeration and your choice of Lazy<T> constructor. There are many ways to handle exceptions while working with lazy patterns. Some of these are as follows:
- No exceptions occur during initialization
- Random exception while initialization with exception caching
- Not caching exceptions
In the subsequent sections, we will try to understand these scenarios in...