6

What are IO Exceptions (java.io.IOException) and what causes them?

What methods/tools can be used to determine the cause so that you stop the exception from causing premature termination? What does this mean, and what can I do to fix this exception?

Victor
  • 3,520
  • 3
  • 38
  • 58
  • 1
    see the error message on the exception instance, it may shed some clues.. but regarding method or tools, what about `try`-`catch`? – Bagus Tesa Sep 06 '18 at 01:05
  • You might want to look at exception handling in JAVA. – Sazzadur Rahaman Sep 06 '18 at 01:07
  • 1
    An IO (Input-Output) Exception is predictably caused by something wrong with your input or output. It can be thrown by most classes in the java.io package for many reasons to prevent errors. For example, using a scanner to read data and receiving an invalid type or writing data into a file that doesn't exist. Surrounding any code that can cause this exception in a try-catch clause will help prevent and find out their cause. – faris Sep 06 '18 at 02:23

4 Answers4

8

Java IOExceptions are Input/Output exceptions (I/O), and they occur whenever an input or output operation is failed or interpreted. For example, if you are trying to read in a file that does not exist, Java would throw an I/O exception.

When writing code that might throw an I/O exception, try writing the code in a try-catch block. You can read more about them here: https://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html

Your catch block should look something like this:

try {
    //do something
}catch(FileNotFoundException ex){
    System.err.print("ERROR: File containing _______ information not found:\n");
    ex.printStackTrace();
    System.exit(1);
}
Code Daddy
  • 118
  • 5
4

Here you go https://docs.oracle.com/javase/7/docs/api/java/io/IOException.html

IOException is thrown when an error occurred during an input-output operation. That can be reading/writing to a file, a stream (of any type), a network connection, connection with a queue, a database etc, pretty much anything that has to do with data transfer from your software to an external medium.

In order to fix it, you would want to see the stack trace of your exception or at least the message, to see exactly where the exception is thrown and why.

try {
    methodThrowingIOException();
} catch (IOException e) {
    System.out.println(e.getMessage()); //if you're using a logger, you can use that instead to print.
    //e.printStackTrace(); //or print the full stack.
}

The error message that will be printed will likely show you what the issue is. If you add the error message here, I'll be able to give you more info on how to fix that specific IOException. Without that, no one can really give you a complete answer.

Stef
  • 2,603
  • 2
  • 18
  • 28
3

It is a very generic exception that a lot IO operation can cause. A best way is to read the Stack Trace. To continue the execution you can use the try-catch block to bypass the exception, but as you mention you should investigate into the cause.

To print the stack trace:

try {
    // IO operation that could cause an exception
} catch (Exception ex) {
    ex.printStackTrace();
}
mckuok
  • 744
  • 6
  • 11
1

IOException is usually a case in which the user inputs improper data into the program. This could be data types that the program can't handle or the name of a file that doesn't exist. When this happens, an exception (IOException) occurs telling the compiler that invalid input or invalid output has occurred.

Like others have said, you can use a try-catch statement to stop a premature termination.

try {
 // Body of code
} catch (IOException e) {
 e.printStackTrace();
}
kPs
  • 21
  • 1