Decoupling: The Exception Puzzle
See how separation of concerns plays a role in dealing with your exceptions within a system.
Join the DZone community and get the full member experience.
Join For FreeIn any system design, "Separation of Concerns," is one of the core principles. This not only keeps the system modular and but keeps the impact of any changes to the minimum, limited to affected module.
Software systems are no different. While architecting an application, it's good practice to divide the application into different layers or tiers where each layer is assigned a specific function. For example, consider the following application with simple layers,
This is the simplest application architecture with neatly divided tiers. In a decoupled, modular system, the changes inside the layers/modules do not affect the other layers that are using it. This generally achieved by defining a fix contract between the layers. In programming languages, this is achieved by Interfaces.
Now, Exceptions, or error conditions, are also part of contract. A layer can throw many exceptions depending on functionalities at the granular level.
For example, a persistent layer can throw different exceptions depending on whether the data being stored is in a database, file system, or some cloud storage.
Then the question is, should a module using another module know all the potential exceptions it can receive?
Let's see if there is a cleaner way out.
Defining a fixed set of exceptions per layer is the starting point. Here, for simplicity, let's assume the following exceptions from each layer:
PersistentException
BusinessException
Here, when the UI layer calls the business layer, it knows that it can potentially encounter BusinessException so it will take care of handling it. The same is true for interactions between the business layer and the Persistent layer.
But How Is Decoupling in the Picture?
Well, let's say a user is saving some data — the request goes via business layer all the way to the database.
Implementation of the persistent layer could be:
interface PersistentLayer{
DataStatus storeData(Data data) throws PersistentException;
}
And let's say we are storing data in a database.
public class PersistentLayerImpl implements PersistentLayer{
DataStatus storeData(Data data) throws PersistentException{
try{
//Code related to DB operation
}catch(ConstraintViolationException e){
Logger.error("Error while storing in DB",e);
throws new PersistentException(e);
}
}
}
The business layer could be using:
public class BusinessLayerImpl implements BusinessLayer{
DataStatus someOperation(Data data) throws BusinessException{
try{
//Business operation etc
persistentLayer.storeData(data); //Calling Persistent Layer
}catch(PersistentException e){
Logger.error("Error while storing",e);
throws new BusinessException(e);
}
}
}
Now, say there is a change in the persistent layer. Say instead of the database, there is a web services call to store the data in the cloud. The persistent layer code could change as follows,
DataStatus storeData(Data data) throws PersistentException{
try{
//Code related web services operation
}catch(WebServicesException e){
Logger.error("Error while storing via web services",e);
throws new PersistentException(e);
}
}
See the effect of this change on the business layer? None at all.
Note that this concept can be extended to n-tier application or modules.
This is one of the ways where the degree of decoupling among layers/modules can be reduced.
Opinions expressed by DZone contributors are their own.
Trending
-
Write a Smart Contract With ChatGPT, Metamask, Infura, and Truffle
-
Preventing Data Loss With Kafka Listeners in Spring Boot
-
How To Scan and Validate Image Uploads in Java
-
Mastering the Art of Building Complex Machine Learning Models: A Comprehensive Guide
Comments