Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Cannot display throw new exception error message

Reply
Thread Tools

Cannot display throw new exception error message

 
 
Matt
Guest
Posts: n/a
 
      07-01-2004
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.jav a:106)
at java.io.FileInputStream.<init>(FileInputStream.jav a:66)
at java.io.FileReader.<init>(FileReader.java:41)
at ExceptionTest6.openFile(ExceptionTest6.java:6)
at ExceptionTest6.main(ExceptionTest6.java:1
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());
}
}

}
 
Reply With Quote
 
 
 
 
Stefan Waldmann
Guest
Posts: n/a
 
      07-01-2004
Matt wrote:
> 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.jav a:106)
> at java.io.FileInputStream.<init>(FileInputStream.jav a: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:1
> 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
 
Reply With Quote
 
 
 
 
Christophe Vanfleteren
Guest
Posts: n/a
 
      07-01-2004
Matt wrote:

> 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.jav a:106)
> at java.io.FileInputStream.<init>(FileInputStream.jav a:66)
> at java.io.FileReader.<init>(FileReader.java:41)
> at ExceptionTest6.openFile(ExceptionTest6.java:6)
> at ExceptionTest6.main(ExceptionTest6.java:1
> 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.

--
Kind regards,
Christophe Vanfleteren
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To throw or to throw not? Emanuele D'Arrigo Python 6 11-15-2008 04:12 PM
forcing new to fail (or throw an exception) H.S. C++ 6 07-26-2007 11:02 PM
All New IIS virtual directories with ASP.NET throw exception "initial character not valid" Pao ASP .Net 12 04-04-2007 05:13 PM
JNI's throw new does not throw an exception yarona@m-sys.com Java 15 09-08-2005 08:36 AM
Throw Exception Vs Throw New Exception Kerri ASP .Net 2 10-27-2003 02:13 PM



Advertisments