Cannot display throw new exception error message

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

}
 
S

Stefan Waldmann

Matt said:
The following program has this output, but I don't understand why it cannot display
"file doesn't exist!" when it calls e.getMessage();

Hello,

you can't see your error message, because your code doesn't reach the
line where you throw your Exception. The system throws it's own
FileNotFoundException where you try to create a new FileReader for a
file which doesn't exist. (Line 6 in your code. The exception stack
trace below even shows you this)

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)
exception was thrown in openFile, line 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"));
exception is thrown here! --^^^
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());
}
}

}

To display your custom message, your openFile() method should look like:

public static void openFile()
{
try {
BufferedReader br = new BufferedReader(new FileReader("88.txt"));
String line = br.readLine();
br.close();
}
catch(FileNotFoundException ex) {
System.out.println("file doesn't exist!");
}
}

If you would have had a look at the FileReader API documentation, you
would know, that trying to create a FileReader for a file that doesn't
exist results in a FileNotFoundException.

<http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileReader.html#FileReader(java.lang.String)>


Stefan
 
C

Christophe Vanfleteren

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

}
The actual exception is caused by what you do on line 6 in your code (you
should look closely at the stacktrace you get, they're not just there to
look pretty), which is the first line in your openFile() method. The
exception gets thrown and is not catched in your method, which is
immediately exited, so your if statement never gets executed.

Just put the contents of your method in it's own try/catch block, and throw
yor own exception from the catch block.

Also, if br is null, why do you try to close it? You'd get a
NullPointerException.

You might also consider posting beginner problems like these on c.l.j.help
instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top