All that adding a throws
does it say that the method can throw an Exception
, you want to be handling an Exception
if it is thrown. To do this, you can wrap the code chunk is a try-catch block. There is no way to stop an Exception
from being thrown if it should be thrown, but the try-catch block will make it so that your program doesn’t crash. The Oracle Tutorial is quite helpful.
For this example, you would do something like this:
try
{
//This already throws FileNotFoundException
br = new BufferedReader(new FileReader(filename));
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
2
solved How to throw FileNotFoundException when using BufferedReader? [closed]