unreported exception FileNotFoundException; must be caught or declared to be thrown SOLVED in Java

unreported exception FileNotFoundException; must be caught or declared to be thrown SOLVED in Java


unreported exception FileNotFoundException; must be caught or declared to be thrown SOLVED in Java

how to solve error: unreported exception FileNotFoundException; must be caught or declared to be thrown FileReader fr=new FileReader(“myfile.txt”) is shown


Content

0.24 -> hi everyone in this video i will be  showing you how to solve the error  
4.64 -> unreported exception file not found exception must  be caught or declared to be thrown so i am getting  
10.8 -> this error when i try to read the content present  in this particular text file and display it  
15.44 -> so i'll be showing you how to solve this  exception all right am having here a really  
18.88 -> really simple code to read the contents of  the file and display it if you want you can  
22.16 -> just pause the video and type it out and in my  case i'm just directly specifying the file name  
27.36 -> because my dot java file and my dot txt file are  present in the same folder alright so actually  
33.44 -> there isn't any error in this code but we are  getting an exception so the simplest way to solve  
38.24 -> this is enclosing our entire code within a try  catch block all right so just come here to the top  
45.92 -> and here you need to specify try open curly  bracket and just enclose the code alright  
53.04 -> so once you're done enclosing it you'll be  having the catch block so it's going to be  
57.2 -> catch and open circular bracket and curly bracket  so here you need to specify exception all right  
64.16 -> so it's going to be exception e so you can  basically print the name of the exception  
68.64 -> you can do this way like system dot out dot  print the exception or you can just have  
78.24 -> some print statement like you can just  have error something like that you can  
82.72 -> do whatever you want i will do with this  so this is now fine so i am now saving it
90.4 -> and recompiling it again
95.68 -> so this time you can see when i compile i'm not  getting any error or exception which is because  
99.76 -> of the try catch block that we have added over  here that's the main reason and if i just now  
105.6 -> run this as well i'll be getting i am java this  text basically is present inside my file over  
112.48 -> here yeah so here you can see i'm having this  text so which means that this program is working  
118.48 -> 100 percent fine and we have now solved this issue  this file not found exception and i hope you have  
124.88 -> found this video useful i've also done many other  programming videos in java c c plus plus node.js  
130 -> react.js mysql mongodb so do check other videos  of my channel subscribe me thanks for watching

Source: https://www.youtube.com/watch?v=HwOz8bvf0Nk