I have been reading about the Chain of Responsibility design pattern which basically allows a decoupling between the sender of a request and receiver of the request. The request can be passed along a chain until someone is ready to consume the request. Now in Java when we specify that a method is capable of throwing an Exception
we let the caller of the method handle the exception. If it is not interested it can further propagate the exception up the chain. So can this process be cited as an application of the Chain of Responsibility design pattern?
Java exception handling is another example of chain of responsibility design. When an error occurs, the exception call will look for a handling class. If there is no handler, the super Exception class will be called to throw the exception. Otherwise, the handler class will handle it.
One of the great example of Chain of Responsibility pattern is ATM Dispense machine. The user enters the amount to be dispensed and the machine dispense amount in terms of defined currency bills such as 50$, 20$, 10$ etc. If the user enters an amount that is not multiples of 10, it throws error.
Generally a try/catch/finally block is written for handling exception which could go terribly wrong very soon. If you are logging exception in catch block you have got it wrong already. If you are trying to recover from the exception and continuing the code flow then you are right.
In object-oriented design, the chain-of-responsibility pattern is a behavioral design pattern consisting of a source of command objects and a series of processing objects.
You are right. Exception handling in Java is based on Chain of responsibility pattern In this pattern:
Hence, Exception handling is a Chain of responsibility pattern
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With