Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6972165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:55:58+00:00 2026-05-27T16:55:58+00:00

i currenty thinking about a concept which should treat illegal states in my java

  • 0

i currenty thinking about a concept which should treat illegal states in my java program.
I have introduced a class which does processing steps on images.

public class ImageProcessor{
    public Image processImage(Image img){
       //do some processing steps
    }
}

Now, I want to introduce another class which should check the the image before doing the processing.

public class ImageChecker{
     public void checkImage(Image img) throws ImageNotProcessableException{
         //for example, if the image has a width of 0 the full process
         //should be broken, throw an exception
         if (img.width=0) throw new ImageNotProcessableException("width is 0");

         //other conditions throwing also exceptions

         // the image is fine, do nothing
     }
}

The two classes should be used in a Coordinator class.

 public class Coordinator{
    public void coordinate(Image img) throws ImageNotProcessableException{
       //initialize both

       imageChecker.checkImage(img); //if no exception is throw the process can run
       imageprocessor.processImage(img);
    }
 }

The question is now, is this way of treating Exception (defining a separate method for them) a bad coding style? The idea for this design was to prevent polluting processing code with exception handling. I want the coordinator to throw exceptions and I thought this could be a senseful way. What do you think, is this a good way or is it an antipattern.
Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:55:59+00:00Added an answer on May 27, 2026 at 4:55 pm

    The validating method itself is a very good idea. You introduce very good separation of concerns – checking preconditions and validation in one place and actual processing in another.

    However the usage is incorrect. The ImageProcessor should eagerly call ImageChecker.checkImage(). Otherwise the client of your library might forget to call it and pass invalid image.

    Also *@Damien_The_Unbeliever* brings up a very good point about the structure of the code.

    To make it as fancy as possible I would create an ImageProcessor interface with two implementations: one that performs the actual processing and a decorator implementation (ImageChecker) that performs validation and passes validated object to target ImageProcessor.

    This way you can either use safe or fast (assuming validation is costly) implementation. Also in the future you might introduce other elements to the chain like caching or profiling.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For about a year I have been thinking about writing a program that writes
We have a continuous integration server running Hudson CI . I'm thinking about putting
I'm currently analyzing a program I wrote in assembly and was thinking about moving
I am currently thinking about some efficiency/speed issues. I have about 40 a elements
I am currently thinking about with what pattern I should solve the following issue.
I am currently thinking about how to design a multithreading system in Java that
I am recently thinking about if throwing constructor from Java is good or not.
Currently thinking about pitching the argument for us migration from vs 2005 (winforms) to
I am currently thinking about open-sourcing a project of mine and am in the
I've been thinking about the deployment process I am currently using, and I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.