开发者

error in the following code

开发者 https://www.devze.com 2023-01-19 16:19 出处:网络
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println(\"Enter the开发者_StackOverflow String:\");
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

精彩评论

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