I appologise for this half arsed question. , How come when you are working with file input/output you use the Throws IOException or similar to that. LIke if you create a file that is already there I think that throws an exception. However there is no catch keyword with instruction what to do when an exception is throw. The program continues to run without you ever knowing an exception was throw ?
IOException is an unchecked exception, which means the compiler won't guarantee that it is checked in you code (meaning you have a try/catch block). So you don't necessarily need to use try/catch, although it is good practice to do so. I hope that helps.
Originally posted by Eric Daly: IOException is an unchecked exception, which means the compiler won't guarantee that it is checked in you code (meaning you have a try/catch block).
Wrong. IOException is a checked exception. Any subclass of Exception (or Exception itself) excluding RuntimeException and it's subclasses are checked exceptions. Any program using a method which throws a checked exception must either catch or declared to be thrown that exception.
Vijitha has already pointed out the error in the first part of the post.
Originally posted by Eric Daly: So you don't necessarily need to use try/catch, although it is good practice to do so. I hope that helps.
It's generally not good practise to try/catch for unchecked exceptions, as their arising is usually an indication of a bug in the code and/or a wrong runtime environment, missing resources and the like.
Only in exceptional cases (no pun) it may be desirable to trap a runtime exception and continue execution, with or without information to the user.
There are no new questions, but there may be new answers.
I agree with NFE, but IME can be prevented by checking for hasNextXXX in Scanner, which is the only class that is using it. And if an exception can be prevented by checking first, I think you should prevent it.
The general rule is this: - unchecked exceptions can be prevented by checks (!= null, hasBoolean(), etc) - checked exceptions are caused when something outside the JVM causes a problem (network disconnect, hard disk failure, etc); checks are usually not possible to detect such issues
There are bound to be exceptions to this rule, but in most cases it does hold.