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 6100973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:26:49+00:00 2026-05-23T13:26:49+00:00

For my classes that have methods with similar input-checking logic (for example, a custom

  • 0

For my classes that have methods with similar input-checking logic (for example, a custom multi-dimensional array that has a lot of methods, all of which check if given coordinates are within the array limits), I create a separate private checker that throws runtime exceptions, and also a public checker, that just returns a boolean value indicating if a variable is acceptable for this class methods. Here’s example:

public class Foo {



    public void doStuff(Variable v) {

        checkVariableUnsafe(v);
        ... // do stuff
    }



    private void checkVariableUnsafe(Variable v) throws InvalidVariableException {...}



    public boolean checkVariable(Variable v) {
        try {
            checkVariableUnsafe(v);
            return true;
        } catch (InvalidVariableException e) {
            return false;
        }
    }
}

Is it OK to use it, or are there some downfalls that I fail to see? What’s the commonly used pattern in such situations?

  • 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-23T13:26:50+00:00Added an answer on May 23, 2026 at 1:26 pm

    It is often recommended to avoid using exceptions for normal program flow. Without debating that issue here, if you wanted to follow that advice, then you could put the logic that actually does the check in the public checkVariable method, and have the private checkVariableUnsafe method call checkVariable and throw an exception if it returns false.

    I don’t think there is enough context in your question to comment definitively on the appropriateness of the API you have created, but I can see nothing intrinsically wrong with it.

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

Sidebar

Related Questions

I have multiple classes that have similar implementation for different named methods: class MyClassX
I have several service classes that have static methods and offer a service to
What do you do with classes that have no member data, only methods? Do
I have classes that store data, and methods to go get data for individual
I have a need for methods in several classes that must always follow a
I have an interface that defines some methods I would like certain classes to
I have a 100 classes that have some similar elements and some unique. I've
In a lot of my PHP projects, I end up with classes that have
I have the following two methods that (as you can see) are similar in
I have two very similar classes that do essentially the same thing. The only

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.