Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

What should be done in the case of a Java invalid input exception?

Asked by Larry Huffer Oct 10, 2022 1.3K views 1 answer
Share

About this question

Should you throw an exception if a method's input values are out of range? eg

//no imaginary numbers

public int MySquareRoot(int x)
{
    if (x<0>    {
    throw new ArgumentOutOfBoundsException("Must be a non-negative integer"); 
    }
    //our implementation here 
}

Now this method should never be called with a non-negative number, but hey programmers make mistakes. Is throwing exceptions here the right thing to do?

Your answer

1 Answer

More Java discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Java Blogs

Guides, tips and career advice on Java from JanBask experts.