M
Matt
The following program has this output, but I don't understand why it cannot display
"file doesn't exist!" when it calls e.getMessage();
D:\javatest\exceptiontest>java ExceptionTest6
java.io.FileNotFoundException: 88.txt (The system cannot find the file specified
)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at java.io.FileReader.<init>(FileReader.java:41)
at ExceptionTest6.openFile(ExceptionTest6.java:6)
at ExceptionTest6.main(ExceptionTest6.java:18)
nice error message = 88.txt (The system cannot find the file specified)
=========================================================
import java.io.*;
public class ExceptionTest6
{
public static void openFile() throws IOException
{ BufferedReader br = new BufferedReader(new FileReader("88.txt"));
if (br == null)
{ br.close();
throw new IOException("file doesn't exist!"); //<== cannot show this!!
}
String line = br.readLine();
br.close();
}
public static void main(String args[]) throws IOException
{ try
{
openFile();
}
catch(IOException e)
{ e.printStackTrace();
System.out.println("nice error message = " + e.getMessage());
}
}
}
"file doesn't exist!" when it calls e.getMessage();
D:\javatest\exceptiontest>java ExceptionTest6
java.io.FileNotFoundException: 88.txt (The system cannot find the file specified
)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at java.io.FileReader.<init>(FileReader.java:41)
at ExceptionTest6.openFile(ExceptionTest6.java:6)
at ExceptionTest6.main(ExceptionTest6.java:18)
nice error message = 88.txt (The system cannot find the file specified)
=========================================================
import java.io.*;
public class ExceptionTest6
{
public static void openFile() throws IOException
{ BufferedReader br = new BufferedReader(new FileReader("88.txt"));
if (br == null)
{ br.close();
throw new IOException("file doesn't exist!"); //<== cannot show this!!
}
String line = br.readLine();
br.close();
}
public static void main(String args[]) throws IOException
{ try
{
openFile();
}
catch(IOException e)
{ e.printStackTrace();
System.out.println("nice error message = " + e.getMessage());
}
}
}