Is the Java exception handling engine an example of a responsibility chain design pattern?

I read about the Responsibility Chain design template, which basically allows you to decouple between the sender of the request and the recipient of the request. The request can be transmitted along the chain until someone is ready to use the request. Now in Java, when we indicate that a method is capable of throwing an Exception , we let the calling method handle the exception. If this is not interesting, it can further propagate the exception along the chain. So can this process be cited as an application of the Chain of Responsibility design template?

+4
source share
1 answer

You're right. Exception handling in Java is based on a chain of responsibility scheme In this template:

  • The sender will not know which object in the chain will serve his request
  • Each node in the chain can decide to execute a request -> catch an exception and
    wrapping it with a special application exception
  • Each node can redirect a request -> exception exception for the caller
  • None of the nodes can fulfill the request -> Leaves the job with the caller

Therefore, exception handling is a responsibility chain template.

+5
source

All Articles