1

My getChoice() method takes a char input from the console and returns the char to main(). Should getChoice() just throw the exception and let main() deal with it:

static char getChoice() throws IOException
{
    BufferedReader br = new 
    BufferedReader(new InputStreamReader(System.in));
    return (char)br.read();
}

Or should getChoice() catch the exception, deal with it and return a char:

static char getChoice() 
{
    BufferedReader br = new 
    BufferedReader(new InputStreamReader(System.in));
    char temp;
    try {
        temp = (char)br.read();
    } catch(IOException exc) {
        System.out.println("Invalid Input");
        temp = (char)0;
    }
    return temp;
}

Which approach is better from the designing perspective? Is there a better way to do this?

Eran
  • 387,369
  • 54
  • 702
  • 768
SouvikMaji
  • 1,088
  • 3
  • 22
  • 39

4 Answers4

2

Throwing an exception seems like the better option to me, since returning a default value (or error code, if you like) in case of an exception does not let the caller know there was an error while reading the input from the user. Using a return value like (char)0 as an error code brings you back to languages that don't have exception handling.

That said, you should consider catching the IOException and throwing some exception of your own, that describes the error.

For example :

static char getChoice() throws UserInputException
{
    BufferedReader br = new 
    BufferedReader(new InputStreamReader(System.in));
    char temp = (char)0;
    try {
        temp = (char)br.read();
    }
    catch (IOException ioEx) {
        throw new UserInputException ("Error reading input", ioEx);
    }
    return temp;
}
Eran
  • 387,369
  • 54
  • 702
  • 768
1

The first one is way better. Exceptions are meant to signal exceptional situations. Return values are not. In the second one, the caller has to be aware that 0 is returned in case of a problem. And moreover, 0 is a valid char value, which makes it impossible to distinguish between "the input is '0'" and "there was an exception".

Not that using a BufferedReader to read a single char is a completely overkill: there is nothing to be buffered.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

It depends on your favorite style. In your particular, very simple case I'd suggest the first approach, i.e. just let exception to be thrown.

Here are the reasons. There are 2 cases:

  1. main does know how to handle the exceptinal case (either thrown exception or returned illegal value)
  2. main has nothing to do with this case.

If main knows how to deal with this case 2 approaches are pretty similar:

// Catching exception
main(String[] args) {
   try {
       char c = getChoice();
       // do A
   } catch (IOException e) {
      // do B
   }
}


// Returning 0
main(String[] args) {
      char c = getChoice();
      if (c == 0) {
          // do B
      } else {
       // do A
      }
}

However if you are returning 0 you actually care about the value twice: first into getChoice(), second time into main() that creates additional coupling between these 2 methods.

However very often even main() has nothing to do with wrong input. In this case your approach is:

main(String[] args) throws IOException {
     char c = getChoice();
     // do A
}

As you can see the code is much simpler. So, your choice depends on the fact whether your caller has reason to catch exception. The reason can be either handle it or wrap it with exception of other type.

AlexR
  • 114,158
  • 16
  • 130
  • 208
0

When should a method throw an exception? Typical advice is to throw them only in exceptional situations. But what counts as an exceptional situation? I suggest a different rule (originally suggested by Herb Sutter, I believe): if, and only if, the alternative would be to fail to do what the method is specified to do. This transforms the question from something vague (what is exceptional) to something potentially precise (what does the specification say).

So what is the specification for your getChoice method? It reads a choice. Consider the case that the method can not read the choice. The method must not return a choice for that case, because it has failed to read the choice. So it must instead throw an exception. When the read operation throws an IOException, the getChoice method has tried but failed to read the choice. So it must throw an exception. Hence, the method should not try to handle the IOException itself, but should let its caller handle it.

Raedwald
  • 46,613
  • 43
  • 151
  • 237