April 13, 2022
Nikolay Kostov (Nikolay.IT)
Topics covered:
- What are Exceptions?
- Handling Exceptions
- The System.Exception Class
- Types of Exceptions and their Hierarchy
- Raising (Throwing) Exceptions
- Best Practices
Video (in Bulgarian)
Presentation Content
What are Exceptions?
- The exceptions in .NET Framework are classic implementation of the OOP exception model
- Deliver powerful mechanism for centralized handling of errors and unusual events
- Substitute procedure-oriented approach, in which each function returns error code
- Simplify code construction and maintenance
- Allow the problematic situations to be processed at multiple levels
The System.Exception Class
- Exceptions in .NET are objects
- The System.Exception class is base for all exceptions in CLR
- Contains information for the cause of the error / unusual situation
- Message – text description of the exception
- StackTrace – the snapshot of the stack at the moment of exception throwing
- InnerException – exception caused the currentexception (if any)
Types of Exceptions
- .NET exceptions inherit from System.Exception
- The system exceptions inherit from System.SystemException , e.g.
- System.ArgumentException
- System.NullReferenceException
- System.OutOfMemoryException
- System.StackOverflowException
- User-defined exceptions should inherit from System.Exception ([more info])
Throwing Exceptions
- Exceptions are thrown (raised) by throw keyword in C#
- Used to notify the calling code in case of error or unusual situation
- When an exception is thrown:
- The program execution stops
- The exception travels over the stack until a suitable catch block is reached to handle it
- Unhandled exceptions display error message
Choosing the Exception Type
- When an invalid parameter is passed to a method:
- ArgumentException , ArgumentNullException , ArgumentOutOfRangeException
- When requested operation is not supported
- When a method is still not implemented
- If no suitable standard exception class is available
- Create own exception class (inherit Exception )
Exceptions: Best Practices
- catch blocks should begin with the exceptions lowest in the hierarchy
- And continue with the more general exceptions
- Otherwise a compilation error will occur
- Each catch block should handle only these exceptions which it expects
- If a method is not competent to handle an exception, it should be left unhandled
- Handling all exceptions disregarding their type is popular bad practice (anti-pattern)!
- When raising an exception always pass to the constructor good explanation message
- When throwing an exception always pass a good description of the problem
- Exception message should explain what causes the problem and how to solve it
- Good: " _Size _ should be integer in range [ 1…15] "
- Good: " _Invalid _ state. First call Initialize () "
- Bad: " Unexpected error "
- Bad: " Invalid argument "
- Exceptions can decrease the application performance
- Throw exceptions only in situations which are really exceptional and should be handled
- Do not throw exceptions in the normal program control flow (e.g. for invalid user input)
- CLR could throw exceptions at any time with no way to predict them
- E.g. System.OutOfMemoryException
Summary
- Exceptions provide flexible error handling mechanism in .NET Framework
- Allow errors to be handled at multiple levels
- Each exception handler processes only errors of particular type (and its child types)
- Other types of errors are processed by some other handlers later
- Unhandled exceptions cause error messages
- Try-finally ensures given code block is always executed (even when an exception is thrown)