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?