简体   繁体   中英

What is a IOException, and how do I fix it?

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?

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);
}

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();
}

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.

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM