Skip to main content

Pattern: Chain

Chain of responsibility pattern allows an object to send a command without knowing what object will receive and handle it.
Chain the receiving objects and pass the request along the chain until an object handles it.

Purpose of .NET implementation

You want to pass the exception to the possible handlers / catch blocks in the all functions in the call stack.
Exception bubbling in .NET exemplifies the Chain of Responsibility pattern by allowing exceptions to be passed along a chain of potential handlers (catch blocks) until one is found that can handle the exception.
This can be useful when you want to ensure that exceptions are handled at the appropriate level of abstraction, rather than being caught and handled at a lower level where they may not be fully understood or properly addressed.
By allowing exceptions to bubble up the call stack, you can ensure that they are handled in a consistent and appropriate manner, regardless of where they occur in the code.
This mechanism decouples the thrower of the exception from the handlers, providing a flexible and dynamic way of managing errors that occur during runtime.

Example in .NET :

Chain

Chain example for Pattern Chain
namespace Chain;

public static class ChainDemo
{
public static int SecondException()
{
try
{
// Calls 'FirstException' method which is known to throw an exception.
FirstException();
return 5;
}
catch (Exception ex)
{
// Throws a new exception, chaining the caught exception 'ex' as the inner exception.
// This adds context to the exception, indicating it originated from 'SecondException'.
throw new Exception($"from {nameof(SecondException)}", ex);
}
}
static int FirstException()
{

throw new ArgumentException("argument");
}
}

Download source

See Source Code for Microsoft implementation of Chain

SourceCode ApplicationBuilderExtensions.UseMiddleware
SourceCode UseMiddlewareExtensions.UseMiddleware

Learn More

C2Wiki
dofactory
DPH
Wikipedia

Homework

Implement a middleware in ASP.
NET Core that intercepts the exception and logs it to the database.
The middleware should be able to pass the exception to the next middleware in the chain.