开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜