Exceptions Best Practices
Fundamentals¶
Read and be familiar with the following:
- Exception Fundamentals
- Using Exceptions Fundamentals
- Exception Handling Fundamentals
- Creating and Throwing Exceptions Fundamentals
- Compiler-generated Exceptions Fundamentals
- Best Practices for Exceptions
- Using Standard Exception Types
Best Practices¶
Engineers should follow Microsoft Exception Best Practises.
Engineers should not throw System.Exception, System.SystemException, System.NullReferenceException, or System.IndexOutOfRangeException
in their own code.
Engineers should throw predefined Exceptions when it is suitable to do so e.g. ArgumentNullException
Engineers should only create Exceptions when predefined ones do not exist, but should prioritise specificity over a less detailed generic predefined exception.
e.g. Create custom OutOfMoneyException
instead of InvalidOperationException("OutOfMoney")
Engineers should only catch specific types of exceptions not a general Exception catch.
Engineers should use finally blocks to clean up resources even if no exceptions have been caught.