Over a million developers have joined DZone.

Decoupling: The Exception Puzzle

DZone 's Guide to

Decoupling: The Exception Puzzle

See how separation of concerns plays a role in dealing with your exceptions within a system.

· Integration Zone ·
Free Resource

In 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,  

Image title

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{

        //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{

        //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{

    //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. 

architechture ,design decision

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}