I'm handling for the first time Exceptions in Java and I want to know if this this the good way to go.
public static void main(String[] args) throws FileNotFoundException {
submethod();
}
static void submethod() throws FileNotFoundException {
Scanner scan = new Scanner(new File("file.txt"));
while (scan.hasNextLine()) {
// do somethig...
}
}
The thing that sounds strange to me is the explicit declaration throws FileNotFoundException
also in the main
method, otherwise the compiler reports:
error: unreported exception FileNotFoundException; must be caught or declared to be thrown
I wanted to know if I'm doing it wrong. In a more complicated project, where you need to catch much more exceptions, it would become very messy. Is this the better practice to handle exceptions? And why do I need to declare it in both methods?