In the code above Scanner input = new Scanner(new File(" "));
indicate that you are trying to open a file.Now this might or might not be available when you are trying to open it.Hence a exception is thrown if it is not available at that time.Hence it is always recommended to handle this code correspondingly.
must be caught or declared to be thrown
indicate that we may expect an exception such as above explained can be thrown by the code and hence must be handled.Exception handling is done using either
1.use try catch
try{
ur code that generates exception
}
catch{
handle exception
}
This illustrates the first part of the exception you got "must be caught"
2.use throw
when you dont want to handle the exception right here you can throw it to the method that is calling it.The calling method must handle this exception.
meth1()
{
meth2();
}
meth2()throws Exception
{
try{
.....
.....
}
catch(Exception e)
{
...
u will not handle it here;
throw e; //exception thrown to the calling method meth1()
}
}
This illustrates the
second part of the exception "declared to be thrown"