• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Exception is not thrown in coressponding try block

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

--------------------------------------------
OutPut:
exception java.io.FileNotFoundException is never thrown in bod
y of corresponding try statement
catch(FileNotFoundException FNFE)
^
rethrow2.java:30: unreported exception java.io.FileNotFoundException; must be ca
ught or declared to be thrown
throw FNFE;

^ 2 errors
 
Marshal
Posts: 76447
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using file input streams for a text file? Use a file reader and buffered reader or a Scanner but don't pass "myfile.txt" to the Scanner constructor.
Multiple println() calls? Append all your chars to a StringBuilder; use this method to get the correct new line characters to append as well. Print the StringBuilder object in one statement. There is a performance overhead to each access to the terminal and you will probably find that will save several milliseconds per character printed, or several seconds per page.
Printing the stack trace and re-throwing the same Exception? Your line 30 throws an Exception, but it is not inside a try, so the following catch cannot catch that Exception. That is why your method should declare that it might throw a file not found Exception. It is also possibly bad to re-throw the same Exception because it will be handled twice.

Campbell's Computer wrote:java rethrow2
java.io.FileNotFoundException: myfile.txt (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at rethrow2.m1(InputDemo.java:24)
at rethrow2.main(InputDemo.java:12)
java.io.FileNotFoundException: myfile.txt (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at rethrow2.m1(InputDemo.java:24)
at rethrow2.main(InputDemo.java:12)


That particular constructor of File Input Stream does declare that Exception:-Compiled for me and ran, with the Exception since I don't have that file (see above).

Try changing all instances of FileNotFoundException to java.io.FileNotFoundException.

Your code is horrible to read because you didn't indent it. Also class names should AlwaysStartWithCapitalLetters and variableNames should alwaysStartLowerCase. Use try with reources instead of the close() call.
 
Why fit in when you were born to stand out? - Seuss. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic