ryekayo
ryekayo

Reputation: 2421

Translating what an exception catches

I have a code snippet I am working on:

public void readFile()
{
BufferedReader reader = null;
BufferedReader reader2 = null;
try 
    {
    reader = new BufferedReader(new FileReader("C:/Users/user/Desktop/testing.txt"));
    reader2 = new BufferedReader(new FileReader("C:/Users/user/Desktop/testNotThere.txt"));
    } 
catch (FileNotFoundException e) 
    {
    System.err.println("ERROR: FILE NOT FOUND!\n");
    }
String line = null;
try {
    while ((line = reader.readLine()) != null) 
        {
        System.out.print(line);
        }
    } 
catch (IOException e) 
    {
    e.printStackTrace();
    }
}   

And while I understand what the first exception the snippet detects: catch (FileNotFoundException e), I am looking to understand what the second exception is looking for while printing the lines of the text file:

catch (IOException e) 
    {
    e.printStackTrace();
    }

Can anyone explain what this second exception is looking for? Furthermore, how can I test to make sure this exception will be thrown in the snippet like I did with creating a second BufferedReader reader2?

Upvotes: 0

Views: 66

Answers (1)

Elton Hoffmann
Elton Hoffmann

Reputation: 110

IOException is thrown when your program is interrupted while reading the file. As you may see, IO stands for "Input/Output" which means reading and writing data on disk. So an exception of that kind means that the system crashed while while doing a reading/writing.

Source: http://docs.oracle.com/javase/7/docs/api/java/io/IOException.html

Upvotes: 1

Related Questions