Results 1 to 3 of 3
  1. #1
    phanikumar is offline Member
    Join Date
    Jun 2015
    Posts
    4
    Rep Power
    0

    Default Exception is not thrown in coressponding try block

    Hi, I am trying execute below code. But I am getting compile time errors. I have written below code to display the contents of the file "myfile.txt".
    But Actually there is no file "myfile.txt". Then an exception "FileNotFound" should be thrown at run time. But the below programme is not compiled.
    Why am i getting compile time errors?
    -------------------------------------------------------------
    code:
    Java Code:
    import java.io.*;
    class rethrow
    {
    public static void main(String args[])
    { //throws IOException
    rethrow rt=new rethrow();
    try
    {
    rt.m1();
    }
    catch(FileNotFoundException FNFE)
    {
    FNFE.printStackTrace();
    }
    }
    void m1()
    {
    try
    {
    FileInputStream fin=new FileInputStream("myfile.txt");
    System.out.println("file contents");
    int ch;
    while((ch=fin.read())!= -1)
    System.out.println((char)ch);
    fin.close();
    }
    catch(FileNotFoundException FNFE)
    {
    FNFE.printStackTrace();
    throw FNFE;
    }
    catch(IOException IOE)
    {
    IOE.printStackTrace();
    }
    }
    }
    -----------------------------------------
    Out put:
    rethrow.java:11: exception java.io.FileNotFoundException is never thrown in bod
    y of corresponding try statement
    catch(FileNotFoundException FNFE)
    ^
    rethrow.java:30: unreported exception java.io.FileNotFoundException; must be ca
    ught or declared to be thrown
    throw FNFE;
    ^
    2 errors

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Exception is not thrown in coressponding try block

    Because your 'm1' method does not say that it throws a FileNotFoundException.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Exception is not thrown in coressponding try block

    Method m1() doesn't state that it can throw a FileNotFound exception and the compiler believes you, so in the try block in the main( ... ) method, no such exception can be thrown. (actually you lied to the compiler because that method can throw such an exception; the compiler starts to protest against that too). Mark that method as 'throws FileNotFoundException' and see what happens.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 5
    Last Post: 05-11-2014, 08:42 PM
  2. wrongly thrown exception
    By dawnMist in forum New To Java
    Replies: 1
    Last Post: 11-29-2012, 01:24 PM
  3. Replies: 5
    Last Post: 02-14-2011, 11:39 PM
  4. After catching the exception thrown
    By scoobyrox in forum New To Java
    Replies: 2
    Last Post: 09-05-2010, 02:29 PM
  5. Which exception is thrown.....
    By money123 in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 03:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •