IOException signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations
Sudhir Srinivasan wrote:1. Why does the IDE not allow IOException to be caught where its thrown
2. once modified to Exception, the exception raised is java.util.InputMismatchException and not java.io.IOException. Why?
Not quite. It receives a String but the String must match an int. You cannot pass 123 to a Scanner. But you can pass "123" which is a String-OK for nextInt(). If, however, you pass "Campbell" or "123.45" or "9999999999999999999999999999" which are all invalid for conversion to an int, you will suffer an InputMismatchException.
Sridhar Santhanakrishnan wrote:. . . A string instead of an int (like in your code) would throw InputMismatchException because there is an obvious mismatch - it expects an int but the input was a String.
IOException is never thrown in body of corresponding try statement error is coming because there is no code within try block which throw IOException.
IDE gives error because when we save (Ctrl+S) any java file in IDE(Eclipse,Netbeans etc), IDE complies that java file for us (means ruuning javac command for us).
I hope you have covered Checked and Unchecked Exceptions topic in your course/training.
IOException is an checked exception which means code is checked during compile time for any piece of code which throws IOException and whether it is properly handled or not. We can either catch a checked exception using catch block OR add a throws clause in method signature.
Now coming to your code, you have added a catch clause for IOException but code within try block do not throw IOException.
To remove this exception, i am just adding a throw clause deliberately in your code. When this is added(as shown in code below) , IDE will not give error.
Reason - we have now deliberately thrown IOException in try block which has been properly handled in catch(IOException) block.
So bottom line is - we are not required to catch IOException until unless it is thrown from code present within try block.
Sudhir Srinivasan wrote:
Thank you Abhay, Rob and Campbell.....
Campbell Ritchie wrote:
You do realise that an IOException is completely different? It might be caused by trying to write to a read-only File, or a loose wire, or some network problem, so you are not getting input or output. You might suffer one of its subclasses instead. If you get an InputMismatchException, you have got some input, but the wrong format.
If you use a Scanner, it consumes any IOExceptions, but there is a method in Scanner which allows you to retrieve its most recent IOException; I think it is called ioException. You can call that method on a Scanner and thorw that Exception (but make sure to use a null test because the throw keyword doesn't react well to nulls ).
Campbell Ritchie wrote:Why on earth are you throwing a new IOException? That is not what the ioException method is for. You check whether it is null, and if it isn't, you can throw it. You don't want a diffrent Exception.
Campbell Ritchie wrote:And never say == true or == false. Write if (flag) ... or if (!flag) ... Not only is that poor style, but also error-prone because you might write = by mistake.
Sudhir Srinivasan wrote:i'm clear in the distinction between = (assignment operator) and == (equals operator).
Sudhir Srinivasan wrote:are there any other IOExceptions besides EOFException, FileNotFoundException?