Synopsis: | Throw exceptions rather than returning some kind of status value |
Language: | C# |
Severity Level: | 4 |
Category: | Exceptions |
Description: |
A code base that uses return values for reporting the success or failure tends to have nested if-statements sprinkled all over the code. Quite often, a caller forgets to check the return value anyhow. Structured exception handling has been introduced to allow you to throw exceptions and catch or replace exceptions at a higher layer. In most systems it is quite common to throw exceptions whenever an unexpected situations occurs. Wrong example: static bool CopyObject(SampleClass original) { if (original == null) { return false; } } Correct example: static void CopyObject(SampleClass original) { if (original == null) { throw new System.ArgumentException("Parameter cannot be null", "original"); } } |
Literature References: |
Aviva AV1200 |