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

  • Home
  • SEARCH
  • 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 644711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:25:22+00:00 2026-05-13T21:25:22+00:00

Lets say have this immutable record type: public class Record { public Record(int x,

  • 0

Lets say have this immutable record type:

public class Record
{
    public Record(int x, int y) {
        Validator.ValidateX(x);
        Validator.ValidateY(y);
        X=x;
        Y=y;
    }

    public final int X;
    public final int Y;

    public static class Validator {
        public void ValidateX(int x) { if(x < 0) { throw new UnCheckedException; } }
        public void ValidateY(int y) { if(y < 0) { throw new UnCheckedException; } }
    }
}

Notice it throws an unchecked exception. The reason is because this is a object that is used quite often and it is inconvenient to have to deal with a checked exception.

However, if this object is in a class library where it may be used to validate user inputs (or some other external input). Now it’s starting to sound like it should be a CHECKED exception, because the inputs are no longer up the to programmer.

Thoughts everyone? should i make checked or unchecked, or are there better design for this?

UPDATE:

my confusion is coming from this scenerio: normally Record would be used like this:

Record r = new Record(1,2);
OtherObj o = new OtherObj(r);

there it’s up to the programmer, so unchecked exception is ok.

However when you get parameters for Record from a user you want to validate them right? so you might call

Record.ValidateX(inputX);
Record.ValidateY(inputY);

There it might throw a checked exception because inputs are no longer controlled?

Sorry, I normally wouldn’t be too concerned with this (personally I think unchecked is fine). but this is actually a problem in a homework and I want to get it right lol.

UPDATE(2):
I starting to think what I need is for ValidateX to throw a checked exception because it is typically what would be used if user input is involved. In that case we could ask the user for input again. However, for the Record constructor, it will throw checked exception because constructing a Record with invalid arguements is an API violation. The new code would look like this:

public class Record
{
    public Record(int x, int y) {
        try
        {
            Validator.ValidateX(x);
            Validator.ValidateY(y);
        }catch(CheckedException xcpt) { throw new UnCheckedException(xcpt.getMessage()); }
        X=x;
        Y=y;
    }

    public final int X;
    public final int Y;

    public static class Validator {
        public void ValidateX(int x) throws CheckedException { if(x < 0) { throw new CheckedException; } }
        public void ValidateY(int y) throws CheckedException { if(y < 0) { throw new CheckedException; } }
    }
}

Now the programmer can validate the parameters before passing them to the Record class. If the does not then it’s a API violation and an unchecked exception is thrown.

How does this sound?!

  • 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-13T21:25:22+00:00Added an answer on May 13, 2026 at 9:25 pm

    You should never validate user input with an unchecked exception. Exceptions are normally checked because that way you can not forget to handle the exceptions.

    Validating parameters used in an api is a completely different kind. These should be unchecked, because otherwise you’ll end up adding try/catch to every function call. Whenever a method is passed an invalid argument, this is surely a programming error. The normal way is to throw an IllegalArgumentException or NullPointerException (or any other that suits your needs).
    In api calls leave the checked excpetions for

    1. Validations you promise to the caller of the api
    2. Expected exceptional conditions (requested file doesn’t exist, write failed etc)

    Besides the above, recoverability is also important for deciding for a checked or unchecked exception. If you can never recover (which is normally the case in a programming error) you can (or should?) take the unchecked exception.

    Preferably don’t write code that doesn’t meet the above guidelines. For you that would mean that you have to write a function that returns a boolean or checked exception when using to validate user input, and an unchecked exception when validating input parameters to your functions. Of course your can and should use the same function to validate the exceptions, just wrap the one that returns a boolean or checked exception:

    public boolean validateX(int x)
    {
         return x > 0;
    }
    
    private void validateParameter(int x)
    {
         if (validateX(x))
         {
             throw new IllegalArgumentException("X is invalid");
         }
    }
    

    This is a bit more work, but it will give you the best of both worlds. By making the validate parameter function private you can ensure you don’t accidentally use it outside of your class. Of course you can also put the if(validateX(x)) ... part inside your constructor.

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

Sidebar

Ask A Question

Stats

  • Questions 439k
  • Answers 439k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's difficult to be certain without a little more information,… May 15, 2026 at 4:50 pm
  • Editorial Team
    Editorial Team added an answer What you did works. Try this: <?php $func = array(… May 15, 2026 at 4:50 pm
  • Editorial Team
    Editorial Team added an answer As I understand it: you started out with the folder,… May 15, 2026 at 4:50 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.