This repository contains the source code and benchmarks referenced in the article "Exceptional Programming in C#", which explores best practices for using exceptions in C# and alternative patterns to improve performance and code clarity.
- Flow Control Best Practices: Why using exceptions for normal flow control should be avoided.
- Performance Costs of Exceptions: Benchmark data illustrating the performance penalties associated with exceptions.
- Alternatives to Exceptions: Implementation of the
Result<T>pattern and the use ofTryParsefor handling predictable errors efficiently.
The code in this repository includes benchmarks created using BenchmarkDotNet to measure the performance impact of exceptions in C#.
- NoTryWithoutExceptionHandling: Code without a
try-catchblock. - TryWithoutExceptionHandling: A
try-catchblock without an exception being thrown. - TryWithExceptionHandling: A
try-catchblock where an exception is thrown and caught. - TryWithExceptionHandlingAndRethrow: Catching an exception and rethrowing with an inner exception.
- TryWithExceptionNestedFunctionHandling: Testing performance with nested function calls to simulate a deeper stack.
- ResultPatternWithSuccess/Failure: Using the
Result<T>pattern to avoid exceptions and compare performance between success and failure scenarios.
To run the benchmarks yourself:
-
Clone the repository:
git clone https://github.com/YourUsername/ExceptionsBenchmarks.git
-
Navigate to the repository directory:
cd ExceptionsBenchmarks -
Ensure you have .NET SDK installed. You can download it here.
-
Run the benchmarks using BenchmarkDotNet:
dotnet run -c Release
Here’s a sample of the results comparing different exception-handling techniques:
| Method | Mean | Error | StdDev | Score |
|---|---|---|---|---|
| NoTryWithoutExceptionHandling | 266.6 ns | 2.53 ns | 2.24 ns | 1 |
| TryWithoutExceptionHandling | 416.8 ns | 2.78 ns | 2.60 ns | 1.56 |
| TryWithExceptionHandling | 26,189.6 ns | 223.43 ns | 186.57 ns | 98.28 |
| TryWithExceptionHandlingAndRethrow | 61,749.5 ns | 410.79 ns | 364.15 ns | 231.61 |
| TryWithExceptionNestedFunctionHandling | 32,893.6 ns | 305.94 ns | 286.18 ns | 123.39 |
| ResultPatternWithSuccess | 197.8 ns | 0.52 ns | 0.49 ns | 0.74 |
| ResultPatternWithFailure | 197.7 ns | 0.41 ns | 0.37 ns | 0.74 |