Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception is not thrown in coressponding try block  RSS feed

 
phaneendra kumar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Marshal
Posts: 55694
163
  • Mark post as helpful
  • send pies
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!