Cannot display throw new exception error message

Discussion in 'Java' started by Matt, Jul 1, 2004.

  1. Matt

    Matt Guest

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

    }
    Matt, Jul 1, 2004
    #1
    1. Advertising

  2. 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.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
    Stefan Waldmann, Jul 1, 2004
    #2
    1. Advertising

  3. 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.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.

    --
    Kind regards,
    Christophe Vanfleteren
    Christophe Vanfleteren, Jul 1, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kerri
    Replies:
    2
    Views:
    12,997
    Kevin Spencer
    Oct 27, 2003
  2. Replies:
    15
    Views:
    7,488
    Roedy Green
    Sep 8, 2005
  3. Pao
    Replies:
    12
    Views:
    1,157
    Juan T. Llibre
    Apr 4, 2007
  4. H.S.
    Replies:
    6
    Views:
    534
  5. Emanuele D'Arrigo

    To throw or to throw not?

    Emanuele D'Arrigo, Nov 14, 2008, in forum: Python
    Replies:
    6
    Views:
    310
    Emanuele D'Arrigo
    Nov 15, 2008
Loading...

Share This Page