-
Exception not caught
Hi,
I am trying to handle an IOException in a class, but when the exception occurs the interpreter is not printing the exception.
the code is as below:-
Code:
import java.io.*;
class SumHelper
{
public String getUserInput(String prompt)
{
String userInput = null;
BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
System.out.println(prompt + " ");
try
{
userInput = is.readLine();
}
catch(IOException e)
{
System.out.println("Please enter a valid response");
}
return userInput;
}
}
for testing the code above i created a test class as below:-
Code:
class test
{
public static void main(String [] st)
{
SumHelper helper = new SumHelper();
String res = helper.getUserInput("Please enter a valid number");
System.out.println(res);
}
}
Here is the output, I am getting
Code:
root@developer:/Coding/JAVA/Sum/testing# java test
Please enter a valid number
root@developer:/Coding/JAVA/Sum/testing#
I didn't entered anything for the exception to be caught, but the program is behaving in abnormal way.
Thanks
Ankit
-
Re: Exception not caught
Hi Ankit,
Giving an empty input by just pressing the enter key doesn't mean that an IOException will be thrown. Because an empty input is a valid value. The IOException will be thrown if there is a failed or interrupted I/O operation.
If you want to get an exception try pressing something like CTRL + BREAK key on your keyboard.
-
Re: Exception not caught
Hi wsaryada,
got your point. :).. thanks