The following asynchronous method is intended to handle potential exceptions from an external service call. Identify the primary issue with this exception handling strategy.
.NET interview question for Advanced practice.
Answer
Returning null swallows the exception, making it impossible for the caller to know that a failure occurred. The exception should be re-thrown or wrapped in a custom exception.
Explanation
Silently catching an exception and returning null (or any default value) is known as 'exception swallowing' and is a dangerous anti-pattern. The caller of GetDataAsync has no way to distinguish between a successful call that legitimately returned null and a call that failed due to a network error. The failure is hidden. A better approach is to let the exception propagate (or wrap it in a custom, more specific exception) so the caller can handle the failure appropriately.