i just learned how to get input from keyboard...'

import java.io.*;
class Fish{
public static void main(String[] args)
      {
BufferedReader Keey = new BufferedReader(new InputStreamReader(System.in));

System.out.println(Keey.readLine());

      }                                                                                          
            }

But whenver i compile it..its shows me the follwing exception..

C:\Program Files\Java\jdk1.6.0_22\bin>javac Fish.java
Fish.java:7: unreported exception java.io.IOException; must be caught or decla
red to be thrown
System.out.println(Keey.readLine());
^
1 error

Recommended Answers

All 7 Replies

If I am not mistaken you have to surround the code inside the method with a try/catch clause, to catch a possible IOException - Do you know how to handle exceptions?

If I am not mistaken you have to surround the code inside the method with a try/catch clause, to catch a possible IOException - Do you know how to handle exceptions?

Yeah. little bit i have done that in c++...
I have tried that too..over here...but not working..by the way..according to error report it was mentioned that there is some exception, ...
well i got some problem with my laptop keyboard..and i am using the usb keyboard..is it..coz of that kb..

nope ..
the problem is that reading input can cause an IOException.
adding a try and catch would most definitely deal with the error you're getting, that's why it says: "IOException; must be caught or decla
red to be thrown"

nope ..
the problem is that reading input can cause an IOException.
adding a try and catch would most definitely deal with the error you're getting, that's why it says: "IOException; must be caught or decla
red to be thrown"

import java.io.*;
class Fistng{
public static void main(String[] args)
      {
BufferedReader Keey = new BufferedReader(new InputStreamReader(System.in));
try{
System.out.println(Keey.readLine());
   }
catch(IOException e)
   {
System.out.prinln("error");
   }                                                                                          
        }
c:\Program Files\Java\jdk1.6.0_22\bin>javac Fistng.java
istng.java:13: reached end of file while parsing
           }→
            ^
1 error

You are missing a closing '}'.

You are missing a closing '}'.

Now its working..apart from that bracket i am also missing t in println..will u tell me..why its working after try and catch statement...as these line have importance only if exception is generated...

Sure. In Java you have two types of exceptions - checked and unchecked. A checked exception is such that the programmer is forced by the compiler to deal with the possibility that the exception will be thrown - IOException is an example for such an exception, and we had to write the try/catch clause in order to deal with that possibility. Exceptions that are not forced by the compiler to be surrounded with try/catch clauses are called unchecked exceptions - IndexOutOfBoundsException is an example for such an exception.

Glad we could help you, please mark the thread as solved :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.