Search code examples
javaioexception

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?


Solution

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