1

I was trying to set my program up that prompts the user to enter the values y or n into a string called resetValue which I try to pass the value as char r. I know there is some errors with the code but I am curious what the error code should be for my catch statement should be. If the user enters an invalid value other than y or n. I was just looking through a list of errors on Oracle website but there are so many I don't know which one is the best fit for this problem.

// Prompts the user to reset the program from the beginning if they wish
while(true){

System.out.print("\nWould you like to enter some new numbers? Type (y) or (n): ");
resetValue = input.readLine();

   try {
      char r = resetValue.charAt(0);
      if( r == y || r == Y ){
         break;
      }
      else if( r == n || r == N){
          reset = true;
          break;
      }
   }
   catch (ERROR STATUS HERE) {
        System.out.print("Please type either (y) or (n) on the keyboard.");
   }
}
Marcus Burkhart
  • 185
  • 1
  • 11

2 Answers2

1

Depending on the nature of readLine, your code should not throw any exceptions save for a run time exception, like NullPointerException. It's not desirable to catch those as those indicate a missed assumption about something with the state of your code (e.g. your input stream has closed somehow).

Omit the try/catch block.

Next, if a user enters an invalid option, give them a chance to correct it. All you need to do is put an else condition (aside from fixing those character literals) and continue the loop until it can break on valid input.

Snippet:

} else {
    System.out.println("Invalid input - please enter only Y or N");
}
Makoto
  • 104,088
  • 27
  • 192
  • 230
-2

Inherit Exception class and create your own Exception!

public class YourException extends Exception 
{
    private static final long serialVersionUID = 1L;  //You need this. Use a random long


    @Override
    public String toString()
    {
        return "Some message";
    }
}
Mar Bar
  • 477
  • 6
  • 11
  • But why?! Why would you want to do this in this case? – Makoto Apr 24 '15 at 23:14
  • Why wouldn't you? Exceptions are just like any other class you can use at your own convenience. – Mar Bar Apr 24 '15 at 23:15
  • But it makes **no** sense to do something like that in this case. Yes, I'm aware that creating a custom exception gives you more convenience, but in a scenario where only a well-defined run time exception could be thrown, it would make **no sense** to throw a custom exception. – Makoto Apr 24 '15 at 23:16
  • If there was a well defined run time exception for the case I agree. Which one would it be for this case? – Mar Bar Apr 24 '15 at 23:20
  • Exceptions are expensive to throw, and once again, they are entirely unnecessary in this scenario. Think about the situation again. The code already has a loop to accept input. It's already checking if it's one of four different values. Why would we want to throw an exception there (since [it really is an expensive thing](http://stackoverflow.com/q/299068/1079354)), when we could just fall back to some message? – Makoto Apr 24 '15 at 23:20
  • Why the downvote? Try Catch are just NOT only for runtime exceptions. They are for values that are not expected in the context. A different value than Y or y is not expected so throwing an exception is perfectly in order and makes code much more readable and structured!!! – Mar Bar Apr 24 '15 at 23:23
  • Throwing exceptions is an expensive thing? I disagree. Can you put it in US dollars? Because I can put in US dollars the amount of money that costs when a programmer has to read unstructured code. Also, can you put it in time units? Come on... – Mar Bar Apr 24 '15 at 23:27