DEV Community

Cover image for Code Smell 26 - Exceptions Polluting
Maxi Contieri
Maxi Contieri

Posted on • Edited on • Originally published at maximilianocontieri.com

3 1

Code Smell 26 - Exceptions Polluting

It is very nice to have lots of different exceptions. Your code is declarative and robust. Or not?

TL;DR; Don't create anemic and empty objects. Even if they are Exceptions.

Problems

  • Over Design

  • Namespace Pollution

Solutions

  1. Avoid creating anemic exceptions as globals.

  2. Create exceptions only if they behave differently.

  3. Model exceptions with objects. Classes are handy for lazy programmers.

Sample Code

Wrong

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class FileReader {

    public static void main(String[] args) {
        FileReader file = null;

        try {
            file = new FileReader("source.txt");
            file.read();
        }
        catch(FileNotFoundException e) {
            e.printStackTrace();
        }
        catch(FileLockedException e) {
            e.printStackTrace();
        }
        catch(FilePermissionsException e) {
            e.printStackTrace();
        }
        catch(IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                file.close();
            }
            catch(IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Right

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class FileReader {

    public static void main(String[] args) {
        FileReader file = null;

        try {
            file = new FileReader("source.txt");
            file.read();
        }
        catch(FileException exception) {
            if (exception.description == (this.expectionMessages().errorDescriptionFileTemporaryLocked() {
                //sleep and retry    
                //IF behaviour is the same with all the exceptions just change the text on object creation and raise the icorrect instance
            }            
            this.showErrorToUser(exception.messageToUser();
             //This example is simplified. Text should be translated
        }                
        finally {
            try {
                file.close();
            } 
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Detection

New exceptions should override behavior methods.

No. code, description, resumable, etc. are not behavioral.

Tags

  • Abuser

  • Naming

Conclusion

You would not create different classes for every Person instance, so they return different names. Why would you do it with exceptions.

How often do you catch a specific exception?.

Go out and check your code.

Is it necessary to be a class?

You are already coupled to the class. Couple to the description instead.

Exception instances should NOT be Singletons.

Relations

Credits

Photo by Nick van den Berg on Unsplash


You will fall to ruin because you believe that exceptions to the rule make new rules.

Pierce Brown


This article is part of the CodeSmell Series.

Last update: 2021/07/13

Sentry image

Make it make sense

Make sense of fixing your code with straight-forward application monitoring.

Start debugging →

Top comments (0)

Gen AI apps are built with MongoDB Atlas

Gen AI apps are built with MongoDB Atlas

MongoDB Atlas is the developer-friendly database for building, scaling, and running gen AI & LLM apps—no separate vector DB needed. Enjoy native vector search, 115+ regions, and flexible document modeling. Build AI faster, all in one place.

Start Free

👋 Kindness is contagious

Dive into this thoughtful piece, beloved in the supportive DEV Community. Coders of every background are invited to share and elevate our collective know-how.

A sincere "thank you" can brighten someone's day—leave your appreciation below!

On DEV, sharing knowledge smooths our journey and tightens our community bonds. Enjoyed this? A quick thank you to the author is hugely appreciated.

Okay