0

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?

ryekayo
  • 2,341
  • 3
  • 23
  • 51
  • Any type of IO error. From the [docs](http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#readLine()): *IOException - If an I/O error occurs* – Jeroen Vannevel Oct 06 '14 at 18:00
  • You can delete your file as it is reading. Or more conveniently: `throw new IOException();` – Jeroen Vannevel Oct 06 '14 at 18:02
  • Check out the answer to this question, it might help clarify for you: http://stackoverflow.com/questions/13216148/java-what-throws-an-ioexception – mdewitt Oct 06 '14 at 18:02
  • @mdewitt thanks, that actually clarifies alot for me. – ryekayo Oct 06 '14 at 18:03
  • http://stackoverflow.com/questions/2629649/java-what-are-ioexceptions-in-bufferedreaders-readline-for – spudone Oct 06 '14 at 18:04

1 Answers1

1

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

Elton Hoffmann
  • 110
  • 1
  • 9