error in the following code
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the开发者_StackOverflow String:");
String str = br.readLine();//error: must be caught or declared
String reverseStr="";
for(int i=str.length()-1;i>=0;--i)
reverseStr += str.charAt(i);
System.out.println(reverseStr);
Should i include try catch block?
The readLine()
method can throw a checked exception (an IOException
to be precise); you have to catch it or declare it in your prototype.
Resources :
- oracle.com - Exceptions
Yes, or throw the exception out of your containing method.
You need to catch/throw exception.
read line can throw IOException - If an I/O error occurs
So it must be taken care of
- Documentation
精彩评论